When you are manipulating the same data in two places at the same time like that, all bets are usually off.

I'd assume that perls internal functions are threadsafe in themselves - what would be the point to build with threads at all otherwise? So the selects wouldn't interrupt or disturb each other. Even without that, you probably would get the expected result anyways, STDOUT ends up being selected last. But you can in no way count on it.

As for the undef $/, I would guess that the same rules apply. Perl internals being thread-safe, it would depend on what you mean by reading.

my @array = <FH>;
Would probably be just fine, even if the undef call comes in the middle of the read.
while( $_ = <FH> ) { ... }
On the other hand, probably would suddenly slurp the rest of the file in - this is multiple IO operations after all. However, I might of course be wrong. :)

That said, you simply do not write threaded code like that - unless you are collecting statistics for chaotic behaviour, standard deviations or something.

Usually you want to stay away from accessing the same data as much as possible, and using threads to do stuff simultaneously, all with their own separate data. But of course, most implementations demand that you share some data, at some point... for instance, a chat engine would be pretty boring if all users wrote to their own separate chatroom. :) This is where locking and controlling access comes in. Take a look at perlthrtut for ways to do that, with semaphore locking on variables, or subroutine synchronization.

Most of the above is void if perl built-in functions are not thread safe. But if you can't trust your perl, then who can you trust? *Grin*


You have moved into a dark place.
It is pitch black. You are likely to be eaten by a grue.

In reply to Re: Threads and FileHandles by Dog and Pony
in thread Threads and FileHandles by robblin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.