in reply to Re^2: loss of dll in a multi threaded app
in thread loss of dll in a multi threaded app

The hash variables below are used so I can have multiple threads waiting in a sleep state for use. The trick to using threads this way, is knowing that they must go to the end of their code block, before joining them. 'go' = 1 wakes them from sleep, and 'die' = 1 signals them to prepare to be joined. It's basically a sleep loop, which wakes up every second, and see's if there is anything to do, or if it's time to die. When done with it's task, it just goes back to sleep. If you know how many max threads you need, you can pre-create them, and track them in some sort of "free-pool", so when you need a thread, it can shift one off of the free-pool-array, and when a thread is finished, push it onto the free-pool-array.

See Tk-with-worker-threads and Threads-w-Perl/Gtk2 demo for full working examples.

sub work{ my $dthread = shift; $|++; while(1){ if($shash{$dthread}{'die'} == 1){ goto END }; if ( $shash{$dthread}{'go'} == 1 ){ #in case I want to pass it code for evaling eval( system( $shash{$dthread}{'data'} ) ); # a simple little task foreach my $num (1..100){ $shash{$dthread}{'progress'} = $num; print "\t" x $dthread,"$dthread->$num\n"; select(undef,undef,undef, .5); if($shash{$dthread}{'go'} == 0){last} if($shash{$dthread}{'die'} == 1){ goto END }; } $shash{$dthread}{'go'} = 0; #turn off self before returning }else { sleep 1 } } END: }

I'm not really a human, but I play one on earth. flash japh