Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Gtk2 ....needs thread testing on a multi-threaded computer

by zentara (Archbishop)
on Apr 03, 2012 at 18:22 UTC ( #963288=note: print w/replies, xml ) Need Help??


in reply to Gtk2 ....needs thread testing on a multi-threaded computer

Well, this is about as far as I'm taking this code. I fixed the locking problem, and some timing issues, but I still get an occaisional segfault. YMMV. I blame the libraries! :-) I had to throw in a goto, and put 2 buttons instead of sharing 1 ( it had something to do with timing). As far as that timing issue goes, I think its rooted in my use of select() delays in the threads, it probably should be done with timers in each thread. Using sleep( or select() delays ) in gui apps always ends up causing problems.

This runs pretty well for me on a multi-core system, with an occaisional core dump or segfault, not many, but enough for me to give up on the code. Its no use kicking a dead horse. :-) Feel free to experiment.

#!/usr/bin/perl use warnings; use strict; use MIME::Base64; use threads; use threads::shared; # apparently the &delete_event sub needs this to exit without GLib err +ors # it depends where you create the thread # ...i'm creating it well down in the Gtk2 code Glib::Object->set_threadsafe (1); use Gtk2 qw/-init -threads-init/; # usage: ./scriptname number_horses # the animated gif support in Gtk2 is a bit limited, and # it will only load from a disk file..... so i include # the base64 encoded image at the bottom, and write it to # disk, if not already done if( ! -e './horse.gif' ){ open (FH, '>', 'horse.gif') or die "$!\n"; print FH get_horsey(); close FH; } my $numworkers = shift || 6; my %threads; share my $finish; $finish = 0; share my $go; $go = 0; share my $die; $die = 0; share my $restart; $restart = 0; my $window = Gtk2::Window->new; $window->signal_connect('destroy', \&delete_event); $window->set( border_width => 15 ); $window->set_size_request(800,600); my $vbox0 = Gtk2::VBox->new(0,0); $window->add($vbox0); my $hbox = Gtk2::HBox->new( 0, 6 ); $hbox->set_size_request(5,50); $vbox0->pack_start($hbox,0,0,0); my $sbutton = Gtk2::Button->new(' Start '); $sbutton->signal_connect( "clicked" => \&start ); $hbox->pack_start($sbutton,0,0,0); my $rbutton = Gtk2::Button->new(' Reset '); $rbutton->signal_connect( "clicked" => \&re_set ); $hbox->pack_start($rbutton,0,0,0); my $lab = Gtk2::Label->new(); $lab->set_markup("<span foreground=\"lightseagreen\" background +=\"black\" size=\"15000\">Race to 500</span>"); $hbox->pack_start($lab,0,0,0); my $ebutton = Gtk2::Button->new('Exit'); $ebutton->signal_connect( "clicked" => \&delete_event ); $hbox->pack_end($ebutton,0,0,0); my $winlab = Gtk2::Label->new(); $winlab->set_markup("<span foreground=\"lightseagreen\" backgro +und=\"black\" size=\"20000\">:Winner</span>"); $hbox->pack_end($winlab,0,0,0); my $wins = Gtk2::Label->new(); $wins->set_markup("<span foreground=\"lightseagreen\" backgroun +d=\"black\" size=\"20000\"> ? </span>"); $hbox->pack_end($wins,0,0,0); my $vbox = Gtk2::VBox->new(); my $sw = Gtk2::ScrolledWindow->new (undef, undef); $sw->set_shadow_type ('etched-out'); $sw->set_policy ('automatic', 'automatic'); $sw->set_size_request (500, 500); $sw->set_border_width(5); $vbox0->pack_start($sw,1,1,0); $sw->add_with_viewport($vbox); my $pba = Gtk2::Gdk::PixbufAnimation->new_from_file ('horse.gif'); foreach my $tr (1..$numworkers){ my $trackframe = Gtk2::Frame->new($tr); $vbox->pack_start( $trackframe, 0, 0, 0 ); $trackframe->set_border_width(3); my $trackhbox = Gtk2::HBox->new( 0, 6 ); $trackhbox->set_size_request(5,50); $trackframe->add( $trackhbox ); my $anim = Gtk2::Image->new_from_animation ($pba); $trackhbox->pack_start( $anim, 0, 0, 0 ); my $scorelab = Gtk2::Label->new(); $scorelab->set_markup("<span foreground=\"black\" size=\"20000\">0</span>"); $trackhbox->pack_end( $scorelab, 0, 0, 0 ); $threads{$tr} = threads->new( \&run, $tr, $winlab, $wins, $trackhbox +, $anim, $scorelab ); } $window->show_all(); $winlab->hide; $wins->hide; Gtk2->main; sub start{ $sbutton->set_sensitive(0); { lock $finish; $finish = 0; lock $restart; $restart = 0; lock $go; $go = 1; } } sub re_set{ $wins->hide; $winlab->hide; { lock $finish; $finish = 1; lock $restart; $restart = 1; lock $go; $go = 0; } $wins->hide; $winlab->hide; $sbutton->set_sensitive(1); } sub delete_event{ foreach my $tr (1..$numworkers){ $go = 0; $die = 1; $threads{$tr}->join; } Gtk2->main_quit; return 0; } sub run{ my ($tr,$winlab, $wins, $trackhbox, $anim, $scorelab ) = @_; $|++; while(1){ if($die == 1){ return }; if ( $go == 1 ){ my $tot = 0; while(1) { my $whip = rand(5) - rand (1); $tot += 5 + (rand 10) + $whip; if ( $restart == 1){ Glib::Idle->add( sub{ $wins->hide; $winlab->hide; $trackhbox->set_child_packing ( $anim, 0, 0, 0 +, 'start'); $scorelab->set_markup("<span foreground=\"black +\" size=\"20000\">0</span>"); } ); goto RESET; } if ( $finish == 0){ if ($restart == 1){goto RESET} Glib::Idle->add( sub{ $tot ||= 0; my $total = sprintf('%.2f', $tot ); $total ||= 0.00; if ($restart == 0){ $trackhbox->set_child_packing ( $anim, 0, 0,$total , ' +start'); $scorelab->set_markup("<span foreground=\"black\" + size=\"20000\">$total</span>"); } #check for winner if($total > 500){ lock($finish); # prevent thread races :-) $finish = 1; $scorelab->set_markup("<span foreground=\"red\" size=\"20000\">$total</span>"); $wins->set_markup("<span foreground=\"red\" size=\"25000\">$tr</span>"); $wins->show; $winlab->show; } }); } RESET: if($die == 1){ return }; if ( $go == 0 ){last } select(undef,undef,undef,.1); } # end of go loop }else{ select (undef,undef,undef,.1) #sleep while loop } } #end of main while loop } sub get_horsey{ return decode_base64( 'R0lGODlhMgAwAMQbAAAAAAAIABgYGBhzGBitGDEAWjExSkpKa0qU/2NCIWtrhHtSKXtaW +oyMlKVr OcbGzs6EWs6cnN6EY+9zAO+ca/cA9/+tGP+te/+9c//nGP///wAAAAAAAAAAAAAAAAAAAC +H/C05F VFNDQVBFMi4wAwEAAAAh/jVCdWlsdCBieSBCbG9veiAtIGh0dHA6Ly9wZXJzby5jbHViLW +ludGVy bmV0LmZyL2xhYmdpYwAh+QQJCgAbACwEAAMALQAsAAAF/uAmjmRplkEanGzrmoGhKMb63r +gYzLSd /6wdzQAAGmG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wGGONjDQo4 +9AAhFf fPMcmaQAScax5JFQRgnlPD48KeWVWDLpQ5ZcRhlHl1e+EAIAIfkECQoAGwAsAwADACwALA +AABf7g Jo5kaZpBGpxs65aBoSjG+t74Fsy0nf+nHc0AABphvBnAIVocWYrDxEK1AFLXFMWR2D5Nj0 +NmPJ48 CppC4QKBJCDbxFek0BwsGXxmooGoC3BdCRdccxsNGg1TE2UKDxgYgV0LCUyGBw92AYwWEw +4NbQ4Q DwsNDw1yhgamdlQTExIXlRASpw8OEhSpcwemDXp7kbEXphACx7u8pgqvrxQYFxQQDg8QGg +IIEruW R71h2hCI1dOOERpdsiJtTUbHB5WxmRq3FA8JlRSxcl0SDhQtlRY4ETGgwQEBovJJk1ftgb +Q2+hbA cYCLxAIuuDBQ0DVggIAGCGlBbLNAXjFRxv7uwdkiyoFEN7iikewo4CBKaS25CMhUZ5YAOR +dj5aJw gdCbh9MSDCBw7CJENhSTbUiAzM1UiRuJsrknTcI0lzQNivK6MamJNwhTqYRwIRcXibFwKq +UpoM3D stxIvFF7TyBRqwuyPlywlAABuPnatM3Lou9Kq1MFe+XScQDcaAm93HgTC8LAqbSK4nRzLz +NOaVIB 3pNqehqcttOG9Uv6+clRov38Ed2yUaaDnwkofZmV1c3KLceNCcCqq3aO5aHZzmrDGScXqi +Eh/wiA T1pzihhD0cL502lqF03tUkAWvPTYaPl+l/YM5Gcu+huOIQsKf6Va7TgcAx9jIgQnympwfH +R2YH3q EThVJakkOMJe9VWim4Ml+LMLAxFE0M58mgGkHYce1lfeWBhuwMA9JXD4BDIJOUgiA4aUAO +M0JzCg AY01kqDfPQzw2GKPAQhQpH7HcLhjkUweM8eRSEYZZJBRIunDDVBWqeWWVf7A5ZdbzgEmly ++EAAA7' ) or die $!; }

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://963288]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (3)
As of 2018-07-20 01:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (422 votes). Check out past polls.

    Notices?