Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
I read that threads::shared variables are not really shared between threads; but rather, each thread has its own copy of the data, and the variables are tied to some "black magic" which propagates changes between threads. If that's true, then I'm very confused by my empirical results. My results seem to indicate the data is actually shared (i.e. not duplicated).
This simple program stores 100 1MB strings in an array shared between 10 threads. If the data is duplicated between threads, that should come out to 1GB total. Instead, the program reports a VmSize of only 214MB:
I also tried adjusting the size of the array and the strings in the array, tried using ints instead of strings, changed the number of threads, and populated the array collaboratively from within the threads. None of that made any difference -- the VmSize was always perfectly consistent with truly shared data (and 11MB fixed overhead per thread.)
Has Perl recently made its threads::shared data truly shared, or am I doing something wrong here? Thanks for any insight!
Highly confused,
Damon Hastings
This simple program stores 100 1MB strings in an array shared between 10 threads. If the data is duplicated between threads, that should come out to 1GB total. Instead, the program reports a VmSize of only 214MB:
So it looks like that 100MB of shared data is really shared! There seems to be an 11MB fixed overhead per thread, which is probably why it reported 214MB instead of 100MB.use threads; use threads::shared; my @a : shared; sub foo { sleep 5; print @a . " elements in \@a\n"; } for (1..10) { threads->new(\&foo); } my $s = " " x 1e6; for (1..100) { push @a, $s; } system "grep VmSize /proc/$$/status";
I also tried adjusting the size of the array and the strings in the array, tried using ints instead of strings, changed the number of threads, and populated the array collaboratively from within the threads. None of that made any difference -- the VmSize was always perfectly consistent with truly shared data (and 11MB fixed overhead per thread.)
Has Perl recently made its threads::shared data truly shared, or am I doing something wrong here? Thanks for any insight!
Highly confused,
Damon Hastings
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: threads::shared variables not really shared... or are they?
by renodino (Curate) on Mar 06, 2008 at 23:51 UTC | |
Re: threads::shared variables not really shared... or are they?
by Joost (Canon) on Mar 06, 2008 at 23:49 UTC | |
Re: threads::shared variables not really shared... or are they?
by zentara (Archbishop) on Mar 07, 2008 at 15:02 UTC | |
by renodino (Curate) on Mar 08, 2008 at 03:21 UTC | |
by zentara (Archbishop) on Mar 08, 2008 at 18:47 UTC |
Back to
Seekers of Perl Wisdom