in reply to Thread in perl

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

Replies are listed 'Best First'.
Re^2: Thread in perl
by priyaviswam (Sexton) on Sep 09, 2011 at 03:51 UTC

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