Next | 3.0 Shared data | Prev |
Data sharing should be explicitly predeclared using:
use threads::shared;
The shared variables should have the special attribute:
my $a : shared = 2; my $b : shared;
It is important to guard parts of the code that accesses and modifies shared variables using lock($var) function:
{ lock($a); lock($b); $b = $a + 2; }
There is no unlock(), the implicit unlocking occurs at the end of the perl block.
There are several modules to make data sharing convenient.
Next | Perl Threads | Prev |