threads::shared - Perl extension for sharing data structures between threads
use threads::shared; my($foo, @foo, %foo); share($foo); share(@foo); share(%hash); my $bar = share([]); $hash{bar} = share({}); lock(%hash); unlock(%hash); cond_wait($scalar); cond_broadcast(@array); cond_signal(%hash);
This modules allows you to share() variables. These variables will then be shared across different threads (and pseudoforks on win32). They are used together with the threads module.
share, lock, unlock, cond_wait, cond_signal, cond_broadcast
share
lock
unlock
cond_wait
cond_signal
cond_broadcast
share takes a value and marks it as shared, you can share a scalar, array, hash scalar ref, array ref and hash ref, share will return the shared value.
share will traverse up references exactly one level. share(\$a) is equivalent to share($a), while share(\\$a) is not.
share(\$a)
share($a)
share(\\$a)
lock places a lock on a variable until the lock goes out of scope. If the variable is locked by another thread, the lock call will block until it's available. lock is recursive, so multiple calls to lock are safe--the variable will remain locked until the outermost lock on the variable goes out of scope or unlock is called enough times to match the number of calls to <lock>.
If a container object, such as a hash or array, is locked, all the elements of that container are not locked. For example, if a thread does a lock @a, any other thread doing a lock($a[12]) won't block.
lock @a
lock($a[12])
lock will traverse up references exactly one level. lock(\$a) is equivalent to lock($a), while lock(\\$a) is not.
lock(\$a)
lock($a)
lock(\\$a)
unlock takes a locked shared value and decrements the lock count. If the lock count is zero the variable is unlocked. It is not necessary to call unlock but it can be usefull to reduce lock contention.
unlock will traverse up references exactly one level. unlock(\$a) is equivalent to unlock($a), while unlock(\\$a) is not.
unlock(\$a)
unlock($a)
unlock(\\$a)
The cond_wait function takes a locked variable as a parameter, unlocks the variable, and blocks until another thread does a cond_signal or cond_broadcast for that same locked variable. The variable that cond_wait blocked on is relocked after the cond_wait is satisfied. If there are multiple threads cond_waiting on the same variable, all but one will reblock waiting to reaquire the lock on the variable. (So if you're only using cond_wait for synchronization, give up the lock as soon as possible)
It is important to note that the variable can be notified even if no thread cond_signal or cond_broadcast on the variable. It is therefore important to check the value of the variable and go back to waiting if the requirment is not fullfilled.
The cond_signal function takes a locked variable as a parameter and unblocks one thread that's cond_waiting on that variable. If more than one thread is blocked in a cond_wait on that variable, only one (and which one is indeterminate) will be unblocked.
If there are no threads blocked in a cond_wait on the variable, the signal is discarded.
The cond_broadcast function works similarly to cond_signal. cond_broadcast, though, will unblock all the threads that are blocked in a cond_wait on the locked variable, rather than only one.
threads::shared is designed to disable itself silently if threads are not available. If you want access to threads, you must use threads before you use threads::shared. threads will emit a warning if you use it after threads::shared.
use threads
use threads::shared
bless is not supported on shared references, in the current version bless will only bless the thread local reference and the blessing will not propagate to the other threads, this is expected to be implmented in the future.
bless
Does not support splice on arrays!
Arthur Bergman <arthur at contiller.se>
threads::shared is released under the same license as Perl
Documentation borrowed from Thread.pm
perl threads
1 POD Error
The following errors were encountered while parsing the POD:
You forgot a '=back' before '=head1'
To install if, copy and paste the appropriate command in to your terminal.
cpanm
cpanm if
CPAN shell
perl -MCPAN -e shell install if
For more information on module installation, please visit the detailed CPAN module installation guide.