in reply to Re: win32 threads problem
in thread win32 threads problem
Module versions you requested:Declare the needed modules to import use IO::Socket::INET; use threads; use constant TIMEOUT => 5; # Bind arguments passed to the code to variables my $tag = shift; my $cfgFilesDir = shift; my $peerPort = shift; # Open the configuration files directory and create a thread for each +configuration file opendir (CDIR, $cfgFilesDir) or die; my $file; my $thr; while ($file = readdir(CDIR)) { if ($file =~ /.+?\.properties/) { $thr = threads->create(\&sendRequest, $file, 'localhost', $pee +rPort ); } } closedir (CDIR); # Wait till all the threads will finish their work. In the threads sub + I implemented a timeout # mechanism to prevent a infinity loop my @threads; my $thread; my $rc; while (@threads = threads->list()) { foreach $thread (@threads) { if ($thread->is_joinable()) { $rc = $thread->join(); # Send the opcmon # $rc->[0] is the return code. # $rc->[1] is the nodeName the thread worked on. &sendOpcmon($tag, $rc->[0], $rc->[1]); } } # To prevent the hard CPU lookups sleep(1); } # sub sendRequest { my ($msg, $peerAddr, $peerPort) = @_; my $eTime = time() + TIMEOUT; my $host = (split/\./, $msg)[0]; # configure the socket. $MySocket = new IO::Socket::INET->new( PeerPort => $peerPort, Proto => 'udp', PeerAddr => $peerAddr, ); ioctl($MySocket, 0x8004667e, pack("I", 1)); # Send the request. $MySocket->send($msg); # Now we will wait till the server will respond to our request. while(time() < $eTime) { $MySocket->recv($text,128); if($text =~ /OK/) { return [1, $host]; } # Sleep for 2 secs just to free up a %CPU usage sleep 2; } # We reached the timeout, probably no response will arrive. return [0, $host]; } # sub sendOpcmon { my ($tag, $val, $object) = @_; my $cmd = "opcmon $tag=$val -object $object"; #qx{$cmd}; print $cmd."\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: win32 threads problem
by BrowserUk (Patriarch) on Dec 07, 2008 at 22:43 UTC | |
by leonidlm (Pilgrim) on Dec 08, 2008 at 07:42 UTC | |
by BrowserUk (Patriarch) on Dec 08, 2008 at 08:10 UTC | |
|
Re^3: win32 threads problem
by zentara (Cardinal) on Dec 07, 2008 at 17:51 UTC |