Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
If I eliminate the threading aspect from my code it works fine. Am I handling the thread creation incorrectly?use threads; use threads::shared; use Thread::Queue; use Wx; my %thread_hash; share(%thread_hash); # Signal Handler $SIG{'KILL'} = sub { # Tell user we've been terminated printf(" %3d <- Killed\n", threads->tid()); # Detach and terminate threads->detach() if ! threads->is_detached(); threads->exit(); }; # .. frame creation etc. # two buttons being called to start/stop thread task EVT_BUTTON( $self, $self->{start}, \&StartExecThread ); EVT_BUTTON( $self, $self->{stop}, \&StopExecThread ); # .. rest of wx code sub StartExecThread { my ( $self, $event ) = @_; my $threadqueue = Thread::Queue->new(); my $worker = threads->create('CallThread', $self, $threadqueue ); $thread_hash{start_queue} = $threadqueue; $self->{start_thread} = $worker; $thread_hash{start_queue}->enqueue('GO'); } sub StopExecThread { my ( $self, $event ) = @_; if ($self->{start_thread}->is_running()) { # Send a signal to a thread print "Killing thread ...\n"; $self->{start_thread}->kill('KILL'); # toggle button state while thread after thread is stopped $self->{start_button}->Enable(1); $self->{stop_button}->Enable(0); } } sub CallThread { my ( $self, $event ) = @_; # toggle button state $self->{start_button}->Enable(0); $self->{stop_button}->Enable(1); # ... do some stuff # toggle button state $self->{start_button}->Enable(1); $self->{stop_button}->Enable(0); }
Thanks, -Paul
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: wxPerl and threads
by zentara (Cardinal) on Nov 23, 2009 at 17:56 UTC |