in reply to Re: Forking Multiple Regex's on a Single String (use threads)
in thread Forking Multiple Regex's on a Single String

Wow ++BrowserUK, this is so cool. I've never done threaded perl before and in fact had to build a threaded perl in my test environment to work with this. I think after some reflection I get how this works, but there are two things I'm not getting.

The first (and biggest) is: how is the printing working? I don't see anything to ensure that writes from two different threads don't "collide" and corrupt the output file. Is that somehow taken care of automatically?

The second (and minor) thing is how to get a limit of, say $limit concurrent threads running. Would something simple like this be appropriate? I just keep a counter of the number of active threads. I do realize I could probably do this smarter by storing the threads in a hash keyed by chromosome or something like that -- just checking if the general approach of run, check for joins, add when joining is complete -- is appropriate.

Update: the code I had before was fatally flawed in so many ways I'm embarrassed. Here's a corrected version (with the original tarnished one at the bottom)

my @waiting = @chromosomes; my %threads; my $max_threads = 10; while ( scalar(@waiting) || scalar( keys(%threads) ) ) { if ( scalar( keys(%threads) ) < $max_threads ) { my $chr = pop(@waiting); $threads{$chr} = threads->new(\&threading_function, $c +hr); } else { foreach my $chr ( keys(%threads) ) { if ( $threads{$chr}->is_joinable() ) { $threads{$chr}->join(); delete $threads{$chr}; } } } }



my $limit = 10; my $processes = 0; my $processed = scalar(@chromosomes); my @threads; while ($processed) { if ( scalar(@threads) < $limit ) { push @threads, threads->new( \&thread, $_ ); ++$processes; } else { foreach my $thread (@threads) { if ( $thread->is_joinable() ) { $thread->join(); --$processes; } } } }

Many thanks again -- you've opened up my eyes/mind to threaded programming!

Replies are listed 'Best First'.
Re^3: Forking Multiple Regex's on a Single String (use threads)
by BrowserUk (Patriarch) on Aug 22, 2006 at 00:55 UTC

    As I mentioned above, if the threaded C-runtime your copy of perl is built against is any good, it will take care of serialising access to global resources (like STDOUT) for you. However, if in practice you find that the output from the threads is getting intermingled, then the following two additional lines to my code above should prevent that (with the caveat that I don't have a multi-cpu machine to test this on):

    ... my %motifs : shared; my $semaphore : shared; ################# Added. ... while ( ($pos = index( $sequence, $str, $pos)) >= 0 ) { lock $semaphore; ################################# Added. print join "\t", $chromosome, $pos, $motif; $pos += $len; }

    There are several ways to approach limiting the number of concurrent threads. I would normally advocate using a pool of threads, but given the size of the data sets being loaded in each thread, discarding them after each use and spawning a new one is probably the safest way of ensuring most of the memory is returned to the heap for re-use.

    Your revised code looks good, but I would suggest a couple of changes.

    1. Continue to loop and join joinable threads even after there are no more files to process.
    2. Check for joinable threads even if it isn't time to start a new one yet.
    3. Insert a sleep into the loop to prevent this thread from running away with the cpu.

    Something like this (untested) code:

    my @waiting = @chromosomes; my %threads; my $max_threads = 10; ## Continue to join threads as the become joinable ## whilst there are still threads running while( @waiting or threads->list( threads::running ) ) { if( keys %threads < $max_threads ) { my $chr = pop @waiting; $threads{ $chr } = threads->new( \&threading_function, $chr ); } ## Do this regardless of whether it's time to start a new thread. foreach my $chr ( keys %threads ) { if( $threads{ $chr }->is_joinable ) { $threads{ $chr }->join; delete $threads{ $chr }; } } sleep 1; ## Prevent this thread consuming all spare cpu }

    Don't get hung up on my changes to your formatting. When you're used to seeing things a certain way, it's easier to follow the logic when the code looks that way. I'm not advocating my preferences here.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.