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.