If you want to see how Glib's eventloop works with threads, here is a little demo.
#!/usr/bin/perl use warnings; use strict; use threads; use threads::shared; use Glib; use Glib qw/TRUE FALSE/; use Term::ReadKey; $|++; ReadMode('cbreak'); #use Gtk2 qw/-init -threads-init/; print "pid-> ",$$,"\n"; #Glib::Object->set_threadsafe (TRUE); #setup shared hash my %threads; my $count = 0; my $main_loop = Glib::MainLoop->new; Glib::Idle->add( sub{ my $char; if (defined ($char = ReadKey(0)) ) { #print "$char->", ord($char),"\n"; #process key presses here if($char eq 'c'){ $count++; print "count $count\n"; share $threads{$count}{'data'}; share $threads{$count}{'die'}; share $threads{$count}{'thread'}; $threads{$count}{'data'} = 0; $threads{$count}{'die'} = 0; $threads{$count}{'thread'} = threads->new(\&start_thread +,$count)->detach; } if($char eq 'x'){ #my $kill = shift @actives; my $kill = 1; if(defined $kill){ $threads{$kill}{'die'} = 1; print "thread $kill killed\n"; $threads{$kill}{'thread'} = undef; }else{print "no threads running\n";} } return TRUE; #keep this going } }); $main_loop->run; ReadMode('normal'); # restore normal tty settings sub start_thread { my $num = shift; my $self = threads->self; print "Thread ", $self->tid, " started\n"; print "idenitifier: $num\n"; while(1){ if( $threads{$num}{'die'} == 1){print "$num dying\n";return} else{ print ' ' x $num, $threads{$num}{'data'}++,"\n"; select(undef,undef,undef,.5); }; } }

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

In reply to Re: Should I use threads? Perl/DHCP/Radius by zentara
in thread Should I use threads? Perl/DHCP/Radius by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.