Threading question, bug or coding problem?



I'm trying to write an application that kicks off several threads to do
things not related to Gtk, and Gtk/Glib seems to be having trouble.  So my
question is whether its a bug in the immature threading model for perl, or
something that I'm not doing correctly.

My program does this: creates a gtk gui, then defines an action to kick
off several threads and monitors them updating a progress bar, pretty
standard stuff.  However, if i thread once it seemingly works no problems,
if i go back and thread again Glib complains and hozes up the gui.

Here is sample code that demonstrates the problem:

--- snip ---
#!/usr/bin/perl -w
use strict;
use threads;

use Gtk2 qw/-init -threads-init/;
use Gtk2::Ex::Simple::List;
die "Glib::Object thread safety failed"
  unless Glib::Object->set_threadsafe(1);

my $window = Gtk2::Window->new('toplevel');
$window->set_default_size(240, 120);

my $vbox = Gtk2::VBox->new(0, 0);
$window->add($vbox);

# Create button to start threading
#
my $thread_button = Gtk2::Button->new("thread");
$thread_button->signal_connect( "clicked" => sub {
        my ($widget) = @_;

        my $tid1 = threads->new("thread_run");
        my $tid2 = threads->new("thread_run");
        my $tid3 = threads->new("thread_run");
        my $tid4 = threads->new("thread_run");
        $tid1->join();
        $tid2->join();
        $tid3->join();
        $tid4->join();
});

# Quit button
#
my $quit_button = Gtk2::Button->new("quit");
$quit_button->signal_connect("clicked" => sub { Gtk2->main_quit; });

$vbox->pack_start($thread_button, 0, 0, 5);
$vbox->pack_start($quit_button, 0, 0, 5);

$window->show_all;
Gtk2->main;

sub thread_run {
  print "This is a thread, doing nothing\n";
  return(0);

}
--- snip ---

Thanks in advance.


BB






[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]