On Thu, Jul 14, 2005 at 01:33:54AM -0700, Michael G Schwern via RT wrote: > > [EMAIL PROTECTED] - Sun Jun 23 02:21:24 2002]: > > > > Having tried to share objects by writing stuff along the lines: > > > > my $a : shared = new FOOBAR; > > > > It took a posting to comp.lang.perl.misc before I realised the error > > of my ways: > > > > my $a = new FOOBAR; > > share($a); > > > > I don't think the threads::shared and perlthrtut docs make this clear > > enough - > > folks coming to ithreads from Java etc are going to finds things a bit > > odd to > > begin with. > > > > SOLUTION: More examples in the docs please, esp. regarding object > > sharing. Also > > maybe trim down some of the general threading info in perlthrtut with > > more > > focus on perl threads and how to use them idiomatically. > > threads::shared and perlthrtut still lacks this additional documentation.
How about this: -- "The greatest achievement of the Austrians has been convincing the world that Hitler was German, and Mozart Austrian." Change 25161 by [EMAIL PROTECTED] on 2005/07/17 20:54:15 [perl #9720] document what can be assigned to a shared scalar Affected files ... ... //depot/perl/ext/threads/shared/shared.pm#38 edit Differences ... ==== //depot/perl/ext/threads/shared/shared.pm#38 (text) ==== @@ -53,6 +53,10 @@ use threads::shared; my $var : shared; + $var = $scalar_value; + $var = $shared_ref_value; + $var = &share($simple_unshared_ref_value); + $var = &share(new Foo); my($scalar, @array, %hash); share($scalar); @@ -101,6 +105,9 @@ C<share> will traverse up references exactly I<one> level. C<share(\$a)> is equivalent to C<share($a)>, while C<share(\\$a)> is not. +This means that you must create nested shared data structures by first +creating individual shared leaf notes, then adding them to a shared hash +or array. A variable can also be marked as shared at compile time by using the C<shared> attribute: C<my $var : shared>. @@ -109,6 +116,20 @@ need to use C<&share([])> and C<&share({})> syntax due to problems with Perl's prototyping. +The only values that can be assigned to a shared scalar are other scalar +values, or shared refs, eg + + my $var : shared; + $var = 1; # ok + $var = &share([]); # ok + $var = []; # error + $var = A->new; # error + $var = &share(A->new); # ok as long as the A object is not nested + +Note that it is often not wise to share an object unless the class itself +has been written to support sharing; for example, an object's destructor +may get called multiple times, one for each thread's scope exit. + =item lock VARIABLE C<lock> places a lock on a variable until the lock goes out of scope.