in reply to A basic 'worker' threading example.
Thanks Preceptor for a great 'template' - great for a person like me who are making my first stumbles in the world of multi-threading in perl! I have been trying to use the template and then instead of pinging, do a numeric operation and return a number. However, I cannot for my life extract the return value. In the example below, I have tried to extract the return value in a variable $sum, which is overwritten at each join... However, I get the following:
Use of uninitialized value $sum in concatenation (.) or string at test +A.pl line 26. sum= Use of uninitialized value $sum in concatenation (.) or string at test +A.pl line 26. sum=
Code:
use strict; use warnings; use threads; use Thread::Queue; my $process_q = Thread::Queue -> new(1,2); sub worker { while ( $process_q -> dequeue() ) { my $retVar=threads -> self() -> tid(); return $retVar; } } $process_q -> end(); #start some threads for ( 1..2 ) { threads -> create ( \&worker ); } #Wait for threads to all finish processing. foreach my $thr ( threads -> list() ) { my $sum=$thr -> join; print "sum=$sum\n"; }
2019-01-23 Athanasius added code and paragraph tags
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: A basic 'worker' threading example.
by roboticus (Chancellor) on Jan 23, 2019 at 17:40 UTC | |
by Anonymous Monk on Jan 24, 2019 at 15:05 UTC | |
Re^2: A basic 'worker' threading example.
by Anonymous Monk on Jan 23, 2019 at 10:29 UTC |