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

I see an issue (actually a crash) on OS X 10.5.5 with my current threading model. see my code snippet below:
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); }
If I eliminate the threading aspect from my code it works fine. Am I handling the thread creation incorrectly?

Thanks, -Paul

Replies are listed 'Best First'.
Re: wxPerl and threads
by zentara (Cardinal) on Nov 23, 2009 at 17:56 UTC
    Am I handling the thread creation incorrectly?

    ... the answer is probably yes..... from my experience with threads under Tk and Gtk2.

    ....the big glaring hotspot in your code, is that you try to create your threads from a button callback.... most of the time that fails, due to the way Perl threads get created.... you should always avoid making threads from a gui callback..... sometimes they work but are often crashers.....

    ...see Reusable threads demo for a way around the problem, by creating your threads before any gui code is invoked.... thread safety with all gui toolkits is a problem....there are alot of workarounds however


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku