userr_1 has asked for the wisdom of the Perl Monks concerning the following question:

I'm using multithreading in a Perl script. However, I get segmentation fault and I believe its when the threads are joined. I'm not sure what I might be doing wrong. I have used print statements to indicate when threads start and end and a print statement in the join block and finally the print statement to indicate the end of the program. Here's a snippet.

#!/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()) { #initiated IPAM session my @retrieved_objs = #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;

Replies are listed 'Best First'.
Re: segmentation fault issue while joining threads in perl
by BrowserUk (Patriarch) on Nov 13, 2014 at 14:51 UTC

    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.

      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)

Re: segmentation fault issue while joining threads in perl
by choroba (Cardinal) on Nov 13, 2014 at 14:41 UTC
    The problem seems to be in #some code. Do you touch the DB or its handles in the sub?
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: segmentation fault issue while joining threads in perl
by FloydATC (Deacon) on Nov 14, 2014 at 12:21 UTC

    Keep in mind that while DBI itself may be thread safe, many DB drivers aren't.

    -- FloydATC

    Time flies when you don't know what you're doing