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

Re: Perl seg fault while joining threads

by BrowserUk (Patriarch)
on Jul 07, 2015 at 22:04 UTC ( [id://1133649]=note: print w/replies, xml ) Need Help??


in reply to Perl seg fault while joining threads

So is there a way to tell perl not to copy a specific variable while creating new threads ?

I don't have Net::AMQP::RabbitMQ, so this is untested, but if your description is accurate, it ought to work.

Just re-structure your code slightly:

#!/usr/bin/perl use Cwd qw/realpath/; use File::Basename qw/dirname/; use lib 'lib'; use threads; use threads::shared; use initial; my @threads = (); my $run :shared = 1; sub proc1 { while($run){ sleep(1); print "I am child thread 1 \n" } } sub proc2 { while($run){ sleep(1); print "I am child thread 2 \n"; } } threads->create(\&proc1); threads->create(\&proc2); my $init = load initial($name); $SIG{'TERM'} = sub { $run = 0; }; while($run){ sleep(1); print "I am main thread\n"; } $_->join() for threads->list();

In general, it is better to start your threads before you create/allocate anything that is only used by your main thread.

See threads: spawn early to avoid the crush. for more details.


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!

Replies are listed 'Best First'.
Re^2: Perl seg fault while joining threads
by kamrul (Acolyte) on Jul 07, 2015 at 22:46 UTC
    There are some places in my code where I cant really create the thread before the instantiating class. Is there any other way ?
      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!

      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://1133649]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found