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

Hi Monks. I am creating a basic implementation using Perl and perl/Tk and i want to implement a feature that i was hoping i could get some assistance with. I have all the GUI implementation using perl tk as one package(say: GUI.pm). And i have another package(Say : connection.pm) where the some tcp connection implementation is done. My requirement is that i need both the thread to be running so that whatever the variable that has been updated from connection.pm should be updated in GUI.pm. Im not very good in threads. Kindly provide your suggestion on this For example : Connection.pm

our @arraylist; if connected push(@arraylist, $clientIP)
GUI.pm
my $pane = $mw->Scrolled('Pane', -bg => 'white', -scrollbars => 'osoe' +) ->place( -anchor=>'nw',-y=>110,-x=>110); my $client; foreach $client (@arraylist){ #$pane->Label(-text => "$client")->pack; $pane->Checkbutton( -text => "$client", -onvalue => 1, -offvalue => 0, #-variable => \$cbvalue[$i], -font => 'big', -bg => 'lightseagreen', )->pack(); }

Replies are listed 'Best First'.
Re: Thread in perl
by zentara (Cardinal) on Sep 05, 2011 at 10:51 UTC
    My requirement is that i need both the thread to be running so that whatever the variable that has been updated from connection.pm should be updated in GUI.pm.

    When a thread updates a shared variable, the main thread will not see the update until it reads the shared variable again. A common way to do this is to have a timer in the Tk GUI to read the shared variable every 10 milliseconds( or whatever rate is good enough for you). You don't show any real code involving sockets, but Simple threaded chat server might help you with some ideas.

    A simple example of a timer based method:

    #!/usr/bin/perl use warnings; use strict; use threads; use threads::shared; # declare, share then assign my $ret; share $ret; $ret = 0; my $val = 0; #create thread before any tk code is called my $thr = threads->create( \&worker ); use Tk; my $mw = MainWindow->new(); my $label = $mw->Label( -width => 50, -textvariable => \$val )->pack(); my $timer = $mw->repeat(10,sub{ $val = $ret; }); MainLoop; # no Tk code in thread sub worker { for(1..10){ print "$_\n"; $ret = $_; sleep 1; } $ret = 'thread done, ready to join'; print "$ret\n"; }

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

      Thanks for the information. I was able to do this by using the perl cond_wait and cond_signal concept.

Re: Thread in perl
by moritz (Cardinal) on Sep 05, 2011 at 10:19 UTC
    Im not very good in threads. Kindly provide your suggestion on this

    Perlmonks is not a code writing service, rather we help you to help yourself.

    So I suggest you learn about threads by reading perlthrtut, try to do what you want, and come back with more specific questions when you're stuck.

      I went through the link. It was very usefule. Even I manage to solve this using cond_signal and cond_Wait concept. Thanks once again for helping to refer this link.

Re: Thread in perl
by Anonymous Monk on Sep 05, 2011 at 10:23 UTC