470,811 Members | 1,160 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,811 developers. It's quick & easy.

Problem with autoflush on Semaphore-threads for Windows

Dear community,

having written a working example for using Semphores on a windows
client, I created a little "server-application" that does the following:

A Server continously "listens/tails" a command-file for new commands.
If a new commands arrives a split of the workload to threads is under-
taken that "control themselves" via Semaphore and are joined finally.

It works.

However, the aspects of logging "I am done" into a log-file does
mysteriously not work synchronised although:
- all threads are definitely joined (see below)
- autoflush is active ($|=1;)

What I observe NOW (see code below) is that the new log-line is written
when a further command arrives OR when the server is interrupted.

Any ideas are highly appreciated. THank you in advance.

Andreas, Paris / France
use threads;
use Win32::Semaphore;
#replaces the http://www.perldoc.com/perl5.8.0/pod/perlthrtut.html
# example of Semaphore (for UNIX)

my $endFlag = undef;
my $logFile = 'D:/www/eclipse/workspace/Perltest/status.log';
my $cmdFile = 'D:/www/eclipse/workspace/Perltest/cmd.log';
my $noThreads = 2; #two threads work at the same time
my $noTasks = 8; #our static workload is 8 tasks
$|=1; #autoflush

open (CMD, "<$cmdFile") || die "error in open:$!";
#seek Handle, Offset_in_bytes, Whence (0=start, 1=cursor, 2=EOF)
seek CMD, 0, 2; #wait at end of file for new incoming commands

open (LOG, ">>$logFile") || die "error in open:$!";
# ">>" positions already automatically to EOF

#note that the position of the sig-handler routine is important !!
$SIG{INT} = sub { print "\n time to say goodbye!\n";
$endFlag = 1; };

while (!defined $endFlag) {
while (<CMD>) {
print "New Command: $_\n";
} #while
print "sleeping for some seconds\n";
sleep 5;
#magic "hopping on the same spot" / Huepfen auf der Stelle
seek CMD, 0, 1; #reposition to "cursor" -> clear EOF-Flag
sub processCmd {
my $para = shift; #para is the command of CMD-file
print LOG "Processing $para"; #write some useful log-info
@th = (); #initialize array of threads

my $s = Win32::Semaphore->new($noThreads,$noThreads);

#We generate all threads
for (1..$noTasks) {
push @th, threads->new(\&myThread, $_, $s);

#..and join them afterwards
for (0..($noTasks - 1)) {
print "join task $_...";
print "done\n";

#xxxxxxxxxx - this log-entry is only written
#xxxxxxxxxx ..after we start the next command OR
#xxxxxxxxxx ..the server is stopped by CTRL-C (INT)
print LOG "Finished $para";
} #sub
# just another dummy thread to do some time-consuming stuff
sub myThread {
my ($p, $s) = @_;

$w = int (rand($p)/3) + 2;
print "i am a thread $p wait for $w seconds\n";
sleep $w;
} #sub

Jul 19 '05 #1
0 3731

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Gianluca Trombetta | last post: by
9 posts views Thread by john smile | last post: by
reply views Thread by m.gelosa | last post: by
2 posts views Thread by iSina | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.