Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: removing all threads..

by flexvault (Parson)
on Dec 09, 2012 at 14:33 UTC ( #1007982=note: print w/ replies, xml ) Need Help??


in reply to removing all threads..

ISAI student,

Normally the existence of a file is not a good way for synchronization. But since you want to "remove all threads" anyway, you could have the 'die' routine create a file in a directory common to all threads, and then within each thread test for the existence of the file and if it exists then exit. This would work with 'fork' or separately started scripts as well.

In the event that the process is going to restart 'immediately', then the master/startup script must ensure that all threads have completed before unlinking the file and restarting the process.

Just a thought, don't use a counter for when to check. Use 'time' to check on the availability of the 'die' file. Check every 5-10 seconds or what is reasonable for your application. I would put a test like this just before waiting on a resource (socket, read, etc.). Also, don't put anything into the 'die' file, since then you could be setting up a potential race condition. If it matters what caused the 'die' then use a separate logging mechanism to record the event. You also may need to set an 'alarm' to break out of wait condition.

Just some ideas!

Good Luck...Ed

"Well done is better than well said." - Benjamin Franklin


Comment on Re: removing all threads..
Re^2: removing all threads..
by BrowserUk (Pope) on Dec 09, 2012 at 14:45 UTC

    Polling on a file is a ridiculous idea.

    If there is any need to poll -- and there usually isn't -- a simple shared variable is far simpler to program and far safer.


    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.

    RIP Neil Armstrong

      BrowserUk,

      Okay...Ed

      "Well done is better than well said." - Benjamin Franklin

      I concur with this method, but I've found it difficult to do in the past without Threads::Shared which has some serious limitations. How else would you share a variable? I'm keenly interested in seeing your methodology...

      As far as the OP's question goes, In the past I've pushed the result of a new thread call onto an array, where I reap all the children on exit by coursing through the array and either waiting on them or sending them a nasty signal if necessary. Threads are tricky business... Especially when you're refactoring someone else's older code where they used threads irresponsibly, or at least without regard to how they get reaped.

      --
      Tommy
      $ perl -MMIME::Base64 -e 'print decode_base64 "YWNlQHRvbW15YnV0bGVyLm1lCg=="'
        but I've found it difficult to do in the past without Threads::Shared which has some serious limitations.

        Hm. Seems simple enough to me?:

        #! perl -slw use strict; use threads; use threads::shared; my $sem :shared; sub tprint { lock $sem; print @_; } my $dieNow :shared = 0; $SIG{INT} = sub { tprint 'sigint seen'; $dieNow = 1; return; }; sub thread { my $tid = threads->tid; my $delay = int rand 10; my $next = time + $delay; while( sleep 1 ) { last if $dieNow; redo unless time > $next; tprint "[$tid] ping"; $next = time + $delay; } tprint "$tid] ending"; } my @threads = map async( \&thread ), 1 .. 10; sleep 1 until $dieNow; $dieNow = 1; $_->join for @threads;

        Outputs:

        C:\test>1007974 [3] ping [9] ping [1] ping [3] ping [2] ping [4] ping [3] ping [1] ping [9] ping [5] ping [3] ping [6] ping [10] ping sigint seen [3] ping 7] ending 2] ending 5] ending 8] ending 1] ending 9] ending 4] ending 6] ending 10] ending 3] ending

        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.

        RIP Neil Armstrong

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1007982]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (14)
As of 2014-07-23 13:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (144 votes), past polls