Update: Sollution/Workaround at bottom.

I have an application using threads that uses system calls. The main thread start up some worker threads and puts work on a queue that's dequeued in the worker threads. The worker threads puts the result on a queue that's dequeued in a non-blocking call in the main thread. The program hangs after a while and the problem seems to be a system call in the worker threads.

I've narrowed the problem down and provide an example that eventually will freeze when run on Windows. The application can run on Linux, but on Windows it hangs quite quick.

use strict; use threads; use Thread::Queue; my $PingQueue = Thread::Queue->new; my $PingResQueue = Thread::Queue->new; my $requestsinprocess = 0; my $ping = 'ping -c 1'; # Ping for Li +nux $ping = 'ping -n 1' if ( $^O eq 'MSWin32'); # Ping for Wi +ndows foreach my $i ( 0..7 ) { # Create some + threads my $pingthread = threads->new(\&Ping,$i)->detach; } while ( 1 ) { # Check for finished pings if ( $requestsinprocess > 0 ) { + # Check if there is anything on PingResQueue while ( defined ( my $pingres = $PingResQueue->dequeue_nb ) ) { + # with a non blocking dequeue of PingResQueue print "$pingres\n"; $requestsinprocess--; } } if ( $requestsinprocess == 0 ) { # Start a new round +with pings foreach ( 0..17 ) { $PingQueue->enqueue("127.0.0.1"); # Put ip on PingQueu +e, it's dequeued in &Ping() $requestsinprocess++; } } select( undef, undef, undef, 0.1 ); # Sleep a while } exit 0; sub Ping { my $id = shift; my $tolalping = 0; while (my $ip = $PingQueue->dequeue) { # Block here and w +ait for an ip on PingQueue my $pingres = `$ping $ip`; # Do a system call $PingResQueue->enqueue("$id " . $tolalping++); # Put something on + PingResQueue to be dequed in the main thread } }

Sollution/Workaround
Instead of using backticks I use open and put locks around the call to open.

$FHLocksemaphore->down; my $pid = open($chldout, "$command |"); $FHLocksemaphore->up; while ( defined ( my $buf = <$chldout> ) ) { $Result[@Result] = $buf; } close($chldout);


In reply to Solved: System call in thread hangs threaded application on win32 by tormod

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, details, 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, summary, 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.