threads-shared - Re: Sharing (or not) %ENV

Posted on Wed Jan 10 17:49:53 2007 by jdhedden in response to 4002 (See the whole thread of 2)
Re: Sharing (or not) %ENV
This is not a 'threads-shared' module problem. It is a Perl bug. %ENV is not a shared variable. It is copied into each thread. The bug is that when 'qx/system' is run in a thread, it does not use %ENV for that thread, but rather %ENV from the main thread. The following illustrates this clearly:
#!/usr/bin/perl use strict; use warnings; $| = 1; use threads; $ENV{'MAIN'} = 'ENV{MAIN} set in main thread'; system('echo MAIN: $MAIN'); system('echo MAIN: $THRD --- this should be blank'); print("\n"); threads->create(sub { system('echo THRD: $MAIN'); $ENV{'THRD'} = 'ENV{THRD} set in child thread'; system('echo THRD: $THRD -- this should NOT be blank'); })->join(); print("\n"); system('echo MAIN: $MAIN'); system('echo MAIN: $THRD --- this should be blank');
which produces:
MAIN: ENV{MAIN} set in main thread MAIN: --- this should be blank THRD: ENV{MAIN} set in main thread THRD: -- this should NOT be blank MAIN: ENV{MAIN} set in main thread MAIN: --- this should be blank
I'll submit a 'perlbug' report on this. In the interim, you'll need to use the workaround you developed.
Write a response