Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

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

by BrowserUk (Patriarch)
on Nov 16, 2009 at 12:41 UTC ( [id://807426]=note: print w/replies, xml ) Need Help??


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

This opens a file, starts 4 threads; writes to that file from all 4 threads (with locking) at random for a short while then stops the threads. It then reopens the file and prints it to stdout:

#! 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( sleep( rand( 1 ) ) && 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;

What more do you need?


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.
  • Comment on Re: [threads] Open a file in one thread and allow others to write to it
  • Download Code

Replies are listed 'Best First'.
Re^2: [threads] Open a file in one thread and allow others to write to it
by Gangabass (Vicar) on Nov 16, 2009 at 13:13 UTC

    Sorry, you didn't understand me (my error). Of course i know that file handler is cloned between threads. But i need more: i need to open file handler in the thread and allow other threads to write to it.

    Or shortly i need a solution to write to log file (from each thread). This is easy. But i'm also need to check for log size. So if it arrive some value i need to close old log file and open new one. And this event (maximum log file) occur in one of the thread (i have shared variable which increment after each write to log).

Re^2: [threads] Open a file in one thread and allow others to write to it
by gulden (Monk) on Nov 16, 2009 at 12:51 UTC

      S'funny, cos the code I posted is very well tested (going back to 5.8.0 and right upto 5.8.9 & 5.10.1 (64-bit)!

      The mistake you're making is reading "Filehandles cannot be made 'shared'." to mean 'filehandles cannot be shared'. Those phrases do not be not mean the same thing.

      Filehandles are process global entities and therefore can be cloned. Which means that each thread created after a filehandle comes into existance gets it's own copy of the global.

      Try the code I posted. Tell me how you get on :)


      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.
        This is our endless discussion. «Filehandles cannot be made 'shared'», maybe in your OS it works, not in the OS's i've tested. When you clone the Filehandle you are assuming that OS will sync the File->writes without any race conditions problems, but that is not true.

        Updated:

        This is what happens when removing the sleep()'s:

        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

        In high concurrency level, the problems appear.

        «A contentious debate is always associated with a lack of valid arguments.»

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (3)
As of 2024-04-20 01:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found