Viki@Stag has asked for the wisdom of the Perl Monks concerning the following question:

I am using threads to create a multi-threaded app ... I now have a script that creates multiple threads that does same task with obviously different parameters, but i want to limit to 2 threads per task. Wait till it completes & issue 2 more .. & so on. How can I limit to 2 threads & how to wait for 2 threads to complete... Thank you.

Replies are listed 'Best First'.
Re: related to threads
by zentara (Cardinal) on Jul 07, 2008 at 11:23 UTC
Re: related to threads
by jethro (Monsignor) on Jul 07, 2008 at 15:12 UTC
    Ah ok. The following should solve your problem, it is tested and works on my machine. Be sure to use a recent version of threads so that 'threads::running' and 'threads::joinable' are implemented.
    while (@dirs_to_ftp) { my $dir= shift @dirs_to_ftp; startthread($dir); while (threads->list(threads::running)>=2) { sleep 1; } foreach my $thr (threads->list(threads::joinable)) { $thr->join(); } } foreach my $thr (threads->list()) { $thr->join(); }
Re: related to threads
by wazoox (Prior) on Jul 07, 2008 at 10:08 UTC
    Warning! Crystal ball failure. Unfortunately, I wasn't able to magically read your mind and see what you tried, how and why it failed. See How (Not) To Ask A Question to get some help. The important part you missed is that you must post an example of your code.
      hi, thanks for consulting the crystalball. I cant post the code ... but here is a better explanation.
      I have a file that contain list of dirs to ftp.
      I have this script to start one thread per dir.
      But this affects the bandwidth, so wanted only two threads at a time.

      Now that i already have a script that generates thread for each dir, I want to know how to modify script to limit only two threads at a time.
      That is my script shud generate a two threads for ftp'ing 2 dirs at a time, and when that finishes generate two more for next 2 dirs... & so on

      I have currently solved the problem by using a subroutine that creates just two threads at a time (& i pass two dirs to this sub).


      Anyways, Thanks
      http://techdiary-viki.blogspot.com/
Re: related to threads
by poolpi (Hermit) on Jul 07, 2008 at 11:57 UTC

    How to manage a thread pool : pool.pl

    hth,
    PooLpi

    'Ebry haffa hoe hab im tik a bush'. Jamaican proverb
Re: related to threads
by jethro (Monsignor) on Jul 07, 2008 at 12:01 UTC
    Maybe it would be better to have a counter that you increment when you start a thread and decrement when that thread stops. Only start a new thread when this number is below 2. This way when one of the 2 threads finishes you can start another one immediately instead of waiting for both threads to stop

    In that case the code should look something like:

    my $i=0; while (@dirs_to_ftp) { my $dir= shift @dirs_to_ftp; startthread($dir); $i++; if ($i==2) { $i= $i- wait_for_thread_finish(); $i--; } } while($i) { $i= $i- wait_for_thread_finish(); $i--; }
    wait_for_thread_finish() should return the number of finished threads, since it might happen that two threads stop simultaneously (depends on your modul/library/thread code). starthread() should only start one thread, not two

    Now if your real question was how to find out when a thread has finished, then you didn't provide enough information. We would at least have to know: What is your operation system ?
    What modul or library are you using to start the threads ?

      Thanks for that jethro, I am using threads module & working on Windows
      http://techdiary-viki.blogspot.com/