Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Perl how to join all threads

by kamrul (Acolyte)
on Jul 06, 2015 at 20:53 UTC ( [id://1133444]=perlquestion: print w/replies, xml ) Need Help??

kamrul has asked for the wisdom of the Perl Monks concerning the following question:

My code is like below:
my @threads = (); $SIG{'TERM'} = sub { foreach(@threads){ $_->join(); } }; push @threads, threads->create(proc1); push @threads, threads->create(proc2);
On TERM signal im trying to wait till all threads are ended. However, whenever I pass TERM signal my program gets terminated with an error "Segmentation fault" How to fix this ?

Replies are listed 'Best First'.
Re: Perl how to join all threads
by BrowserUk (Patriarch) on Jul 06, 2015 at 21:40 UTC

    Try it this way. Put your cleanup code in an END{} block and just exit on the signal. (You'll need to cause your threads to terminate; hence $sig :shared; ):

    #! perl -slw use strict; use threads; use threads::shared; my $sig :shared = 1; $SIG{'INT'} = sub{ print 'sigint'; $sig = 0; exit; }; my @threads = map{ async{ sleep 1 while $sig }; } 1 .. 4; sleep 1 while 1; END{ print 'joining...'; $_->join for @threads; print 'joined.'; } __END__ C:\test>junk39 sigint joining... joined.

    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.
    I'm with torvalds on this Agile (and TDD) debunked I told'em LLVM was the way to go. But did they listen!
Re: Perl how to join all threads
by Anonymous Monk on Jul 06, 2015 at 21:27 UTC
      Hi.. thanks for your reply. I just found the problem is causing due to one thread. I have no problem if I turn that off. The function proc1 is using Thread::Queue. I tested without proc1 and it doesnt create any problem. Code of proc1 is like the below:
      sub proc1 { $q = Thread::Queue->new(); while($p = $q->dequeue()){ if($p eq 'exit'){ last; } ..... } $q->end(); threads->exit(); }

        This: $q->dequeue() blocks internally on a cond_wait(), if no data is available in the queue. Signals will not interrupt threads::cond_wait().

        That said, how will that queue ever receive anything?

        It is created inside the thread and immediately goes into a dequeue(). As it was created inside the thread, no other thread has a handle to it, so no data can ever be queued to it.

        Your design is broken.


        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.
        I'm with torvalds on this Agile (and TDD) debunked I told'em LLVM was the way to go. But did they listen!
Re: Perl how to join all threads
by sundialsvc4 (Abbot) on Jul 07, 2015 at 17:39 UTC

    Also note one of BrowserUK’s comments, found in the “do not mix” thread cited above, that you can sometimes just detach the threads and let them go.   You do not need to join with them unless you need to know that they have all terminated.

    As a categorical rule-of-thumb, do not attempt to do anything within a signal handler, because you have no way to know precisely what was going on at the time the signal was received.   (Although the Perl interpreter will safely maintain its own internal state.)   Your handler should only raise some kind of yellow-flag, and maybe nudge threads that might be dozing so that they will see that the flag has been raised and they can react to it.   Time-consuming loops should include tests of the flag and break out of the loop.   (Another way to do it is to throw an exception.)   Only the main thread should be relied-upon to receive and act on the signal.   The flag itself (as shown elsewhere) must be thread-safe.   The signal handler may be called any number of times.

    The signal should immediately cause some kind of acknowledgement message to be printed (to STDERR) so that the user will know that the program heard the bell ring.   The user should then see additional messages that show that it is, in fact, winding-up its affairs and shutting down.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1133444]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-03-19 04:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found