Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: Perl seg fault while joining threads

by kamrul (Acolyte)
on Jul 07, 2015 at 22:46 UTC ( [id://1133652]=note: print w/replies, xml ) Need Help??


in reply to Re: Perl seg fault while joining threads
in thread Perl seg fault while joining threads

There are some places in my code where I cant really create the thread before the instantiating class. Is there any other way ?
  • Comment on Re^2: Perl seg fault while joining threads

Replies are listed 'Best First'.
Re^3: Perl seg fault while joining threads
by BrowserUk (Patriarch) on Jul 08, 2015 at 07:16 UTC
    Is there any other way ?

    Yes. If you spawn a thread early, it will not copy anything created after it is spawned. If you then arrange for it to spawn the threads that you need to spawn later, they will inherit the cleanliness of their parent. Ie. Won't duplicate anything created after that first thread was spawned.

    The code below looks complicated, but it is really quite straight forward. If you wrap it into a module and call it before you call your initial module, the api can be pretty transparent.

    This just demonstrates the technique; you can do the wrapping to suit your code/style:

    #! perl -slw use strict; use threads; use threads::shared; use Thread::Queue; my $run :shared = 1; sub func1 { print "func1: @_"; sleep 1 while $run; } sub func2 { print "func2: @_"; sleep 1 while $run; } my $Qspawn = new Thread::Queue; async { while( my $parms = $Qspawn->dequeue ) { my( $type, @args ) = split $;, $parms; if( $type == 1 ) { $Qspawn->enqueue( threads->new( \&func1, @args ) ); } elsif( $type == 2 ) { $Qspawn->enqueue( threads->new( \&func2, @args ) ) } else { warn "Unknown type: $type"; } } }->detach; ## now create/allocate your main thread stuff # require initial; # initial->import( entrypoints ); ## if needed # my $init = ... ## When you want to start func1 $Qspawn->enqueue( join $;, 1, 1, 'two', 3.3 ); my $thread1 = $Qspawn->dequeue; ## other stuff ## Now spawn func2 $Qspawn->enqueue( join $;, 2, 2, 'four', 8.8 ); my $thread2 = $Qspawn->dequeue; ## do stuff ## tell func1 & func2 to finish $run = 0; ## Join them $_->join for $thread1, $thread2; ## Let the spawner thread clean itself up exit; __END__ C:\test>spawner.pl func1: 1 two 3.3 func2: 2 four 8.8

    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.
    I'm with torvalds on this Agile (and TDD) debunked I told'em LLVM was the way to go. But did they listen!
Re^3: Perl seg fault while joining threads
by Myrddin Wyllt (Hermit) on Jul 08, 2015 at 01:32 UTC

    Is there a reason that the call to 'initial' needs to be in the main thread? Could you not just put it in it's own thread and spawn that first?

Log In?
Username:
Password:

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

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

    No recent polls found