in reply to Threading and join/termination question
This seems to work right for me. Remember, it's up to the system as to which thread gets run first, unless you prioritize them. So run the code below multiple times, and see how the system sometimes gets the order right, but will sometimes lets a different thread order. Also you should lock @results
#!/usr/bin/perl -w use strict; use threads; use threads::shared; use Thread::Semaphore; my $sem = Thread::Semaphore->new(15); # max 15 threads my @results:shared; for my $i (0..5) { $sem->down; my $t = threads->create(\&mySubName, $i); } <>; print join(" ",@results); <>; sub mySubName { threads->detach(); my $foo = shift(@_); print "$foo\n"; lock @results; push (@results,$foo); $sem->up; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Threading and join/termination question
by cormanaz (Deacon) on Jul 31, 2014 at 18:57 UTC | |
by choroba (Cardinal) on Aug 01, 2014 at 13:11 UTC |