in reply to Issue with Threads while collecting command output

But this program hangs

The only thing I can see wrong, which may cause hanging, is the way you wait for the thread joins to occur.

foreach(@array){ push @threads, threads->create(\&execute_ls, $_); } # when you loop thru @threads to join, join will wait for each thread to finish and becoming joinable foreach (@threads) { $_->join(); } # better would be foreach (@threads) { if ( ( $_ -> is_joinable) { $_->join(); } } alternatively you can detach your threads, instead of joining them, and write your return values to shared variables

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

Replies are listed 'Best First'.
Re^2: Issue with Threads while collecting command output
by BrowserUk (Patriarch) on Nov 08, 2011 at 14:10 UTC
    The only thing I can see wrong, which may cause hanging, is the way you wait for the thread joins to occur.

    Since he is waiting for all threads to finish, the order in which they are joined is irrelevant.

    And using your method, if the first thread (or the first two or the first three) are not yet ready when he tests to see if they are joinable, they will never be joined because your for loop will end and you never go back to re-try them.

    You would have to code that as:

    while( @threads ) { foreach (@threads) { if ( ( $_ -> is_joinable) { $_->join(); } } }

    But that would be pointless.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Just checking to see if you had your coffee today...;-), I'm glad you caught that for-while loop oversight.

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