Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: Explicit thread context

by jdhedden (Deacon)
on May 23, 2006 at 16:46 UTC ( [id://551200]=note: print w/replies, xml ) Need Help??


in reply to Re: Explicit thread context
in thread Explicit thread context

Why are you forced to specify in advance the context that you'll be using?
As threads execute asynchronously, the context must be set before the thread starts executing because it may call wantarray at any time during its execution, and may even finish before a ->join() call is made on it.

Prior to threads v1.31, the context would be determined by how the ->create() call was made. This lead to the following counter-intuitive syntax if you wanted the threads object, but were expecting to get a list back from the thread:

my ($thr) = threads->create('foo'); ... my @results = $thr->join();
Additionally, this Perl bug has threads created in void context, but then tries to get return values from the thread.

With threads 1.31, you can now specify the context directly so as to avoid such confusion:

use threads 1.31; my $thr = threads->create({'list'=>1}, 'foo'); ... my @results = $thr->join(); threads->create({'context'=>'scalar'}, 'bar'); ... foreach my $thr (threads->list()) { my $rc = $thr->join(); }
Does threads throw an assertion error if you accidentally use a different context than the one you promised you'd use?
No, but you may not get the results you expect.

Remember: There's always one more bug.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-19 01:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found