Jarkko Hietaniemi > perl-5.7.3 > threads::shared

Download:
perl-5.7.3.tar.gz

Dependencies

Annotate this POD (3)

Module Version: 0.90   Source   Latest Release: perl-5.8.8

NAME ^

threads::shared - Perl extension for sharing data structures between threads

SYNOPSIS ^

  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);

DESCRIPTION ^

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.

EXPORT ^

share, lock, unlock, cond_wait, cond_signal, cond_broadcast

FUNCTIONS ^

share VARIABLE

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.

lock VARIABLE

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 will traverse up references exactly one level. lock(\$a) is equivalent to lock($a), while lock(\\$a) is not.

unlock VARIABLE

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.

cond_wait VARIABLE

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.

cond_signal VARIABLE

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.

cond_broadcast VARIABLE

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.

NOTES ^

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.

BUGS ^

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.

Does not support splice on arrays!

AUTHOR ^

Arthur Bergman <arthur at contiller.se>

threads::shared is released under the same license as Perl

Documentation borrowed from Thread.pm

SEE ALSO ^

perl threads

syntax highlighting: