in reply to Re: Threads: How to share a FileHandle to Write
in thread Threads: How to share a FileHandle to Write

The output result is wrong... Each FileHandle in each thread have its own context, we can't print to it concurrently.
-bash-3.00$ perl threads_file.pl START 1 START 2 1>LOAD EXIT START 3 START 4 START 5 START 6 2>LOAD EXIT 3>LOAD EXIT ... 59>LOAD EXIT START 61 60>LOAD EXIT START 62 61>LOAD EXIT START 63 62>LOAD EXIT START 64 63>LOAD EXIT 64>LOAD EXIT STOP: Tue May 19 11:51:32 2009
63778 out.txt

Replies are listed 'Best First'.
Re^3: Threads: How to share a FileHandle to Write
by BrowserUk (Patriarch) on May 19, 2009 at 11:22 UTC

    It looks like your C-runtime libraries or Perl implementation is broken(1).

    I cannot reproduce your results on my platform:

    The only logical thing for CLONE to do for an open filehandle is dup() or dup2(), and according to the man pages for those calls:

    NAME dup, dup2 - duplicate a file descriptor SYNOPSIS #include <unistd.h> int dup(int oldfd); int dup2(int oldfd, int newfd); DESCRIPTION dup and dup2 create a copy of the file descriptor oldfd. After successful return of dup or dup2, the old and new descrip +tors may be used interchangeably.
    They share locks, file position pointers and flags; for example, if the file position is modified by using lseek on one of the descriptors, the position is also changed for the other.
    The two descriptors do not share the close-on-exec flag, howeve +r.

    1. That is, your platform is exhibiting unspecified--thought clearly defined--behaviour.


    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.
      I've tested in more than one SO, and the problem is the same. Try to increase the number of threads, in order to increase concurrency and you will get the same result.
        I've tested in more than one SO, and the problem is the same. Try to increase the number of threads, in order to increase concurrency and you will get the same result.

        Here is one copy running 1000 concurrent threads:

        C:\test>junk4 -NTHREADS=1000 STOP: Tue May 19 16:26:26 2009 1000000 1242746771.5932.out

        And this is 20 concurrent copies each running 100 concurrent threads each:

        C:\test>for /l %i in ( 1,1,20 ) do @start /b junk4 -NTHREADS=100 STOP: Tue May 19 16:07:35 2009 100000 1242745650.6976.out STOP: Tue May 19 16:07:35 2009 100000 1242745651.0876.out STOP: Tue May 19 16:07:36 2009 100000 1242745651.15.out STOP: Tue May 19 16:07:37 2009 100000 1242745651.7896.out STOP: Tue May 19 16:07:48 2009 STOP: Tue May 19 16:07:48 2009 STOP: Tue May 19 16:07:49 2009 STOP: Tue May 19 16:07:50 2009 STOP: Tue May 19 16:07:51 2009 STOP: Tue May 19 16:07:51 2009 STOP: Tue May 19 16:07:51 2009 STOP: Tue May 19 16:07:51 2009 STOP: Tue May 19 16:07:52 2009 STOP: Tue May 19 16:07:52 2009 STOP: Tue May 19 16:07:52 2009 STOP: Tue May 19 16:07:52 2009 100000 1242745654.972.out 100000 1242745654.8784.out 100000 1242745656.0952.out STOP: Tue May 19 16:07:52 2009 STOP: Tue May 19 16:07:52 2009 STOP: Tue May 19 16:07:52 2009 STOP: Tue May 19 16:07:52 2009 100000 1242745656.142.out 100000 1242745656.1576.out 100000 1242745657.1092.out 100000 1242745657.1716.out 100000 1242745657.9048.out 100000 1242745657.5304.out 100000 1242745657.3432.out 100000 1242745657.2652.out 100000 1242745656.5476.out 100000 1242745655.596.out 100000 1242745655.8768.out 100000 1242745654.7536.out 100000 1242745654.3012.out

        Nary a whiff of a problem. Still unconvinced it works fine here?

        Is that other OS (I assume that's what you meant by SO?), based on the same kernel? Or compiled against the same CRT? Or both?


        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.