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;

In reply to segmentation fault issue while joining threads in perl by userr_1

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.