1: The time is 1258377733.481062 1: The time is 1258377733.481164 1: The time is 1258377733.481337 1: The time is 1258377733.4 3: The time is 1258377733.473267 3: The time is 1258377733.473595 3: The time is 1258377733.473749 3: The time is 1258377733.473885 3: The time is 1258377733.474008 3: The time is 1258377733.474110 3: The time is 1258377733.474212 3: The time is 1258377733.474313
Your code without the sleep()'s:
#! perl -slw use strict; use Time::HiRes qw[ time sleep ]; use threads; use threads::shared; our $THREADS ||= 4; my $sem :shared; open LOG, '>', 'log.txt' or die $!; sub logit { lock $sem; return printf LOG @_; } sub thread { my $tid = threads->self->tid; my $stop = shift; warn $tid, ": starting:", time(), " Stoptime: $stop\n"; while( time() < $stop ) { logit "%2d: The time is %f\n", $tid, time; } warn $tid, ": stopping at ", time(), "\n"; } my @threads = map threads->create( \&thread, time() + int( rand 10 ) ), 1 .. $THREADS; warn "threads started; waiting\n"; $_->join for @threads; warn "threads done\n"; open LOG, '<', 'log.txt' or die $!; print while <LOG>; close LOG;
My position in this forum is to share ideas,opinions,knowledge,... and of course, "to learn", with the help of all the monks. And only joining strengths it will be possible to achieve real knowledge. Sometimes this forum is used to humiliate members in order to praise to himself, which is a pity.
In reply to Re^10: [threads] Open a file in one thread and allow others to write to it
by gulden
in thread [threads] Open a file in one thread and allow others to write to it
by Gangabass
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |