Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Update: I, like the following commenters, don't think that threads are appropriate in this case, but I'll answer the question in case someone comes looking for an answer where threads would be appropriate.

My problem is that i don't see how i can check if ANY of the 4 workers has finished.
Have a single control queue shared between the worker threads and the main thread.

Create a Thread::Queue object, which we'll call the "control queue". When you create the workers, pass that queue to each of the workers you create. When a worker is done it should enqueue a "worker N done" message into the "control queue".

In your main thread, once all the worker threads are started, do a blocking dequeue from the "control queue" in while a loop, where the loops ends when you've received the appropriate number of "worker N done" messages. (ie: You could receive other useful messages on the same queue).

In the body of the while loop, you can deal with what should happen when ANY of the workers finish (use a condition variable if you're only interested in doing so for the first).

After the loop, you can deal with what should happen when ALL of the workers have finished.

re: threads and system():

Also, you seem to be doing system() (which is fork() and exec()) inside the worker thread. Are you sure you understand the implications of mixing fork() with Perl threads?

-David


In reply to parallelisation (how to wait on N threads) by erroneousBollock
in thread Parallelization of heterogenous (runs itself Fortran executables) code by Jochen

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (2)
    As of 2020-07-09 23:19 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?