Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
more useful options
 
PerlMonks  

Re^2: threads, file handles and wxPerl

by sdetweil (Sexton)
on Feb 13, 2013 at 17:02 UTC ( #1018578=note: print w/ replies, xml ) Need Help??


in reply to Re: threads, file handles and wxPerl
in thread threads, file handles and wxPerl

thanks.. using the fileno() gets me the results I need.. (if you clone the handle as input when u need to write to it!. well you get what you get!)
this is the output approach

open my $fh, ">&=$CHILD_FILENO";


Comment on Re^2: threads, file handles and wxPerl
Download Code
Re^3: threads, file handles and wxPerl
by BrowserUk (Pope) on Feb 13, 2013 at 17:12 UTC

    Be careful. The file won't close until you close both handles; but you have to ensure than you do not close the first handle before the the thread has had a chance to clone it. You may need a shared var as a semaphore to coordinate things.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    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.
      thank you. I added close $fh; after the write in the event handler. I am letting the thread ending close the open3() created handles cause they were declared as local(). Should I do that explicitly? (adding it didn't cause any issues);
        Should I do that explicitly?

        No. Implicit closing works fine. Where the issue arises is typified by this TCP server constuction:

        sub clientHandler { my( $fileno ) = shift; open my $client, ">&=$fileno" or die $!; while( <$client> ) { ...; } } while( my $client = $svr->accept ) { threads->create( \&clientHandler, fileno( $client ) ); }

        The problem is that will work much of the time, especially when testing under light loads, because the thread will succeed in running to the point when the client handle is duped, before the thread that spawned it reaches the end of the while loop and the original $client is auto-closed.

        But then when the program is used under moderately heavy loads, the spawning thread gets another timeslice before the spawned thread has chance to perform the dup, and by the time it does, the handle has been auto-closed and there is nothing to dup.

        It becomes necessary to add a semaphore to prevent the parent thread from reaching the end of the while loop before the child has duped the handle. Not especially difficult; but there are many ways to do it wrongly.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2014-04-19 20:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (483 votes), past polls