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.

In reply to Re^2: Explicit thread context by jdhedden
in thread Explicit thread context by jdhedden

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.