Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
So i have one problem: how to pause all running threads (and continue them after some user actions)?

Add a shared variable initialised to false, and have the main loop within your thread code check it each time around.

When the user clicks the pause button, the action sets the shared var to true.

When the thread-loops next check the shared var, and it is true; they just sleep a little and loop until it becomes false again.

To resume, the button sets the shared var false again, and off they all go.

my $paused :shared = 0; sub thread { while( my $domain = $queue->dequeue ) { sleep 1 while $paused; ## the rest of the thread code. } } ... $pauseButton->onclick( sub { if( $paused ) { $paused = 0; $self->settext( "Pause" ); } else { $paused = 1; $self->settext( "resume" ); } } }; ...

Simple and reliable.

Beware the "threads signals", they are essentially useless.

As they do not interupt running code as real signals do, you therefore have to arrange to poll them, in order to notice that you've been signalled. At which point, all the ridiculous and buggy code that underlies them becomes completely redundant because you're better off just polling a shared var as above.


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.

In reply to Re: How to pause multithreaded application? by BrowserUk
in thread How to pause multithreaded application? by Gangabass

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-25 20:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found