http://www.perlmonks.org?node_id=860078

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

I need a way to pause multithreaded Tkx application (threads are collecting some data via HTTP). Threads push data to the queue so the main application can read it (and show to the user). Customer want to pause the program if one of the threads find some data. By pause i mean stop updating text log widget. Here is code:
sub start { my @result; $queue= Thread::Queue->new; $queue_processed = Thread::Queue->new; my @domains = get_domains($domains_filename); $queue->enqueue(@domains); my @threads= map { threads->create( sub { create_thread($_) } ) } +( 1 .. $CONFIG{NUMBER_OF_THREADS} ); $_->detach for (@threads); my $counter = 0; while ( $counter < scalar @domains ) { my $result = $queue_processed->dequeue_nb; if ($result) { $txt_processed_domains->configure(-state => "normal"); $txt_processed_domains->insert_end( "$result" ); $txt_processed_domains->see("end"); $txt_processed_domains->configure(-state => "disabled"); Tkx::update(); $counter++; } } Tkx::tk___messageBox( -message => "Completed!" ); }
So i have one problem: how to pause all running threads (and continue them after some user actions)?

Replies are listed 'Best First'.
Re: How to pause multithreaded application?
by BrowserUk (Patriarch) on Sep 14, 2010 at 10:24 UTC
    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.
Re: How to pause multithreaded application?
by zentara (Archbishop) on Sep 14, 2010 at 10:22 UTC
    So i have one problem: how to pause all running threads (and continue them after some user actions

    I would think you would need to create a shared variable, which all threads monitor in their code, to instruct the threads to keep working, or go into a looping sleep state. It would require you to rewrite your thread code to use a more manual style, rather than queue. See Reusable threads demo for an example of "go control"


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: How to pause multithreaded application?
by Corion (Patriarch) on Sep 14, 2010 at 10:01 UTC

    I'm not sure what your threads are supposed to do with the currently running requests. I would let them finish their current request and have them recheck a global, shared flag whether to pause or (re)start processing. A more convoluted alternative would be to $queue->unshift dummy jobs on the threads work queue that tell each thread to pause.

    If the request really is "stop whatever you're doing", have a look at the "suspend" example in threads. It seems to use signals, but as the signals are claimed to stay within the confines of Perl and seem not to be OS signals, these should be saner/safer to use with threads.

    If the request only means "don't update the widget", that's easy. Have your threads send the updates to the main program through a Thread::Queue, and then either fetch or discard the log updates from that queue.

Re: How to pause multithreaded application?
by Anonymous Monk on Sep 14, 2010 at 10:06 UTC
    I would rewrite program logic to be able to run using POE::Loop::Tk (of course you'd have to create POE::Loop::Tkx

      Well, you're a twat then aren't you.

      Let's see. You want to be able to carry a little more luggage when you take your family on holiday in the family saloon.

      Do you:

      1. Fit a roof rack and/or box?
      2. Buy an 18-wheel tractor/trailer?
        You sir, are no Pope, silent updates notwithstanding