kamrul has asked for the wisdom of the Perl Monks concerning the following question:
I have a code similar to the below. I have one main script which is calling another module named initial.pm. initial.pm opens up connection with an AMQP server (In my case RabbitMQ)and using Net::AMQP::RabbitMQ library for establishing the connection. Everything works fine except when I try to join my threads I get segmentation fault.
I think the Net::AMQP::RabbitMQ is not thread safe. But this is only being used by the main thread. Im pretty sure you can reproduce the error if you just copy and past the codes below.
main.pl
lib/initial.pm#!/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; my $init = load initial($name); $SIG{'TERM'} = sub { $run = 0; }; threads->create(\&proc1); threads->create(\&proc2); while($run){ sleep(1); print "I am main thread\n"; } $_->join() for threads->list(); 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"; } }
If I call the initial class after creating the threads I dont see the error. It seems the threads are copying all the previously initiated variables into it when it is being created. In my case I dont need to access my $init variable from any other threads. So is there a way to tell perl not to copy a specific variable while creating new threads ?package initial; use Net::AMQP::RabbitMQ; use Cwd qw/realpath/; use File::Basename qw/dirname/; my $mq; my $stop = 0; sub load { my $class = shift; my $self = {}; connectq(); bless $self,$class; return $self; } sub connectq { $mq = Net::AMQP::RabbitMQ->new(); my ($host,$port,$user,$pass) = ('localhost','5672','guest','guest' +); $mq->connect($host, { user => $user, password => $pass, port => $port, timeout => 10, }); $mq->channel_open(1); $mq->consume(1, 'logger'); } 1;
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Perl seg fault while joining threads
by BrowserUk (Patriarch) on Jul 07, 2015 at 22:04 UTC | |
by kamrul (Acolyte) on Jul 07, 2015 at 22:46 UTC | |
by BrowserUk (Patriarch) on Jul 08, 2015 at 07:16 UTC | |
by Myrddin Wyllt (Hermit) on Jul 08, 2015 at 01:32 UTC | |
Re: Perl seg fault while joining threads
by stevieb (Canon) on Jul 07, 2015 at 21:50 UTC | |
A reply falls below the community's threshold of quality. You may see it by logging in. |
Back to
Seekers of Perl Wisdom