in reply to Re^2: Why isn't this code thread-safe? (MCE!)
in thread Why isn't this code thread-safe? (Is "require" thread-safe??)
Hello vr,
One can try loading IO::Handle before spawning workers. That alone is helpful for increasing reliability for modules that involve IO::*. I was able to reproduce threads failing but not after adding the IO::Handle line. Even tested with 100 threads without a delay between them.
use warnings; use feature 'say'; use threads; use Thread::Queue; use IO::Handle; # <-- important my $q = Thread::Queue-> new; my @gang = map async( sub { while ( defined( my $f = $q-> dequeue )) { require Compress::Zlib; say threads-> tid; } }), 1 .. 8; $q-> enqueue( $_ ) for 1 .. 8; $q-> end; $_-> join for @gang;
Kind regards, Mario
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Why isn't this code thread-safe? (MCE!)
by marioroy (Prior) on Nov 19, 2018 at 22:25 UTC | |
|
Re^4: Why isn't this code thread-safe? (MCE!)
by vr (Curate) on Nov 20, 2018 at 16:48 UTC |