TU all for suggestions, the code looks pretty much 
ok now, throws a bogus error at the end but is
generally ok , :)


the code looks like this now :)
-----------------------------------
#warning
#this app kills 
#the cpu somewhat 
#dead, needs a more
#sensitive sleep in
#mainloop
#handle with care on slow
#machines, threads-> yield
#makes it a bit lighter
#but still, use caution

use strict;
use Tk;
use threads;
use threads::shared;
use POSIX;

# for debugging 
$|=1;

# constructing a new window
my $mw = new MainWindow('title'=>"Tiitel");

my $clockdisplay = $mw->Label;
$clockdisplay->pack('side'=>'top');

#for changing the clock value ;-)
my $clockstr : shared ;
$clockstr = 0; 

# indicates when to stop
my $running : shared = 1;
#starting the other thread
# which should change our title
my $ch = threads->create("clocker");

# this loops the tk events and refreshes 
# the window inner things like buttons etc.
# it blocks until the window is closed
predixLoop();

# program ending ;0
$running = 0;
$ch->join();


# SUBS

# the main thread loop
# tries to handle event,
# DoOneEvent(2) means that 
# any action should be 
# takin (from tcl.h) TCL_DONT_WAIT 
# or smth like that , exits the 
# same way as Tk::MainLoop

sub predixLoop {        
        my $oldclockval = "0";
        while(Tk::MainWindow->Count) {
                Tk->DoOneEvent(2);              
                # nice 
                if ($clockstr != $oldclockval) {
                        $oldclockval = $clockstr;
                        $clockdisplay->configure('text'=>$oldclockval);
                }
                threads->yield();
        }
}

# the child thread running loop
# updates the clock after every 1 sec
sub clocker {
        print "clocker starting \n";    
        while($running) {
                $clockstr = time();
                sleep(1);
                threads->yield();
        }
        print "child out \n";
}

# i'm such a bad perl programmer :'-(


Reply via email to