in reply to What is a shared variable?

A shared variable is shared among all threads, not cloned.

Dave.

Replies are listed 'Best First'.
Re^2: What is a shared variable?
by llancet (Friar) on Dec 29, 2009 at 13:14 UTC
    Thanks!
Re^2: What is a shared variable?
by Anonymous Monk on Dec 31, 2009 at 06:46 UTC
    I tried to test this, and on my machine shared version appears to take more working memory
    #!/usr/bin/perl -- use threads; use threads::shared; my $foo = 0 x ( 100 * 1024 ); share($foo); threads->create(sub { $foo.= 1 x ( 100 * 1024); })->join(); sleep 10;
      Shared variables are implemented using a system similar to tying. In each thread there is a lightweight object which when accessed, sets a lock then sets or retrieves the value stored in the "real" variable of which there is one shared across all threads. Depending on the usage, one or more threads may have a current or stale copy of the variable's value cached.

      If you shared an array of strings instead, you'd see less usage with shared array, since only one element of the array might be cached at any one time.

      Dave