Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: threads::shared variables not really shared... or are they?

by zentara (Archbishop)
on Mar 07, 2008 at 15:02 UTC ( #672790=note: print w/ replies, xml ) Need Help??


in reply to threads::shared variables not really shared... or are they?

I'm not an expert on thread memory fundamentals, but I did find something interesting. If you add the 1M scalars to the array from within the threads, IT WILL gain the memory you seek. I had to reduce the number from 100 to 50, because at 100, it was killed by the kernel.

#!/usr/bin/perl use threads; use threads::shared; my @a : shared; for (1..10) { sleep 1; threads->new(\&foo); } sleep 1; system "grep VmSize /proc/$$/status"; <>; sub foo { my $s = " " x 1e6; for (1..50) { push @a, $s; } print @a . " elements in \@a\n"; }

I'm not really a human, but I play one on earth. Cogito ergo sum a bum


Comment on Re: threads::shared variables not really shared... or are they?
Download Code
Replies are listed 'Best First'.
Re^2: threads::shared variables not really shared... or are they?
by renodino (Curate) on Mar 08, 2008 at 03:21 UTC
    The original added 100 1Mbyte strings to @a. Your example adds 50 * 10 = 500 1Mbyte strings to @a. Also, each thread will create a 1Mbyte private scalar; the original only created a single big string.

    (The lack of locks is a bit troubling, but the aforementioned global interpretter lock is probably keeping your example out of trouble)


    Perl Contrarian & SQL fanboy
      Ooops, you are right, this one gives memory use that agrees with the OP's original script. I think the sleep calls prevent the locking problem. At least on a faster machine. :-)
      #!/usr/bin/perl use threads; use threads::shared; my @a : shared; for (1..10) { sleep 1; threads->new(\&foo); } sleep 1; system "grep VmSize /proc/$$/status"; <>; sub foo { for (1..10) { push @a, " " x 1e6; } print @a . " elements in \@a\n"; }

      I'm not really a human, but I play one on earth. Cogito ergo sum a bum

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://672790]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (10)
As of 2015-07-07 18:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (93 votes), past polls