in reply to segmentation fault issue while joining threads in perl

This version of your code works perfectly (as shown by the output at the end):

#!/usr/bin/perl #use DBI; use POSIX; #use Data::Dumper; #use Fcntl ':flock'; use strict; use warnings; use threads; use threads::shared; #use "some IPAM system's package name"; my $host = "1.1.1.1"; my $database = "some_db"; my $user = "some_user"; my $pswd = "some_pswd"; my @threads; #my $dbh = DBI->connect("DBI:mysql:$database;host=$host", $user, $pswd + ,) || die "Could not connect to database: $DBI::errstr"; #$dbh->{'mysql_auto_reconnect'} = 1; my $sql = qq{ #a select statement }; #my $sth = $dbh->prepare($sql); #$sth->execute(); my $rows = 0; my @uni_array; #while(my @request = $sth->fetchrow_array()) for( 1 .. 10 ) { #initiated IPAM session my @retrieved_objs = ( 1..10 ); #searching for some results push @uni_array, @retrieved_objs; foreach(@retrieved_objs) { $rows++; } } my $number = $rows/10; my $ceil = ceil($number); for ( my $count = 1; $count <= $ceil; $count++) { my $t = threads->new(\&sub1, $count); push(@threads,$t); } foreach (@threads) { my $num = $_->join(); print "done with $num\n"; } print "End of main program\n"; sub sub1 { my $num = shift; print "started thread $num\n"; #some code sleep($num); print "done with thread $num\n"; return $num; } #$sth->finish(); #$dbh->disconnect; __END__ C:\test>junk28 started thread 1 started thread 2 started thread 3 started thread 4 started thread 5 started thread 6 started thread 7 started thread 8 started thread 9 started thread 10 done with thread 1 done with 1 done with thread 2 done with 2 done with thread 3 done with 3 done with thread 4 done with 4 done with thread 5 done with 5 done with thread 6 done with 6 done with thread 7 done with 7 done with thread 8 done with 8 done with thread 9 done with 9 done with thread 10 done with 10 End of main program

Which means that the problem lies not with the threading, but with that you've omitted (ie.#some code), and since we are not clairvoyant, we can't really help with that!

(My guess would be that this package: "some IPAM system's package name" is not thread-safe; but it is just a guess.)


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.

Replies are listed 'Best First'.
Re^2: segmentation fault issue while joining threads in perl
by userr_1 (Initiate) on Nov 13, 2014 at 15:45 UTC

    Thank you. Now, I know where to debug :)

      Just in addition to what BrowserUK said, if you are unable to modify the package causing the problem, then move it from a use statement in the parent thread to a require statement in the child threads - Chances are good this will resolve your issue (assuming you don't need access to the package in the main thread)