Thread::Stack - thread-safe stacks adapted from Thread::Queue
use Thread::Stack; my $s = new Thread::Stack; $s->push("foo", "bar"); my $bar = $s->pop; # The "foo" is still in the stack. my $foo = $s->pop_nb; # returns "foo", or undef if the stack was empty my $size = $s->size; # returns the number of items still in the stack
A stack, as implemented by Thread::Stack is a thread-safe data structure much like a list. Any number of threads can safely add or remove elements to or from the beginning of the list. (Stacks don't permit adding or removing elements from the middle of the list).
Thread::Stack
The new function creates a new empty stack.
new
The push method adds a list of scalars on the top of the stack. The stack will grow as needed to accommodate the list.
push
The pop method removes a scalar from the top of the stack and returns it. If the stack is currently empty, pop will block the thread until another thread pushes a scalar.
pop
The pop_nb method, like the pop method, removes a scalar from the top of the stack and returns it. Unlike pop, though, pop_nb won't block if the stack is empty, instead returning undef.
pop_nb
undef
The size method returns the number of items still in the stack.
size
The author of Thread::Queue deserves any credit here. I simply modified Thread::Queue to implemnet a stack interface.
threads, threads::shared, Thread::Queue
To install Thread::Stack, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Thread::Stack
CPAN shell
perl -MCPAN -e shell install Thread::Stack
For more information on module installation, please visit the detailed CPAN module installation guide.