Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^11: [threads] Open a file in one thread and allow others to write to it

by BrowserUk (Pope)
on Nov 16, 2009 at 19:21 UTC ( #807540=note: print w/ replies, xml ) Need Help??


in reply to Re^10: [threads] Open a file in one thread and allow others to write to it
in thread [threads] Open a file in one thread and allow others to write to it

Hum, "buffered IO" and "buffer-fulls"

Remember what I was saying about buffering?

Your modification of my code, mildy modified:

#! 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 $!; my $old = select LOG; $|++; select $old; 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";

When I run that under *nix:

mehere@mehere-desktop:~/perl$ perl -v This is perl, v5.10.0 built for x86_64-linux-gnu-thread-multi ... mehere@mehere-desktop:~/perl$ wc -l log.txt 173746 log.txt

It created a 173000 line file that is flawless!


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^12: [threads] Open a file in one thread and allow others to write to it
by gulden (Monk) on Nov 16, 2009 at 20:02 UTC
    This is the solution 807525, until proven otherwise.
    A contentious debate is always associated with a lack of valid arguments.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://807540]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (11)
As of 2016-08-26 15:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The best thing I ever won in a lottery was:















    Results (372 votes). Check out past polls.