[Israel.pm] Lexical veriable copyed with new thread

Shmuel Fomberg semuelf at 012.net.il
Tue Dec 25 09:29:04 PST 2007

>From: Peter Gordon
>Sent: Tuesday, December 25, 2007 8:17 AM
>Quote from "perldoc threads"
>The threads API is loosely based on the old Thread.pm API. It is very
>important to note that variables are not shared between threads, all
>variables are per default thread local.  To use shared variables one
>must use threads::shared.
>As I read it, the above paragraph says that _nothing_ is copied except
>for those variables that are explicitly shared. The two explanations
>seem to be in conflict - is anyone up to resolving them?

The answer is simple - all the variables are copied, not shared. So the are
local to the thread, because they where copied. 
To share a variable means that the two threads see the same value. And if
one will change the value, the other will see the new value. On default, the
variable are copied so the other thread will not see the change in his copy.

But if you will write:
my $var : shared = 0;
then the two threads will share the variable.

Good Night,

More information about the Perl mailing list