in reply to wait for threads to end in parallel

I ended up having each thread picking files of the array untill it's empty instead of creating a new thread for every file.

A solution suggested by a fellow monk here

Thanks anyway !
  • Comment on Re: wait for threads to end in parallel

Replies are listed 'Best First'.
Re^2: wait for threads to end in parallel
by ikegami (Patriarch) on Mar 16, 2010 at 16:19 UTC
    Ah, like this?
    use threads; use threads::shared; my @file_list :shared = @ARGV; my $num_workers = 2; my %MSG :shared; sub process { my ($file) = @_; print "Opening $file\n"; if(some condition){ lock(%MSG); #write stuff to hash } print "Closing $file\n"; } for (1..$num_workers) { async { for (;;) { my $file = do { lock @file_list; return if !@file_list; shift(@file_list) }; process($file); } } } $_->join() for threads::list();

    This will work as long as you never add anything to the file list once the threads have started. (If you do, you might have worker threads exiting prematurely.)

    Thread::Queue does the same, but hides a couple of the details.