Hi all

I have written a simple multithread perl script which has to stopped when it receives a INT signal.

Most of the time, the execution terminated with a "perl: double free or corruption".

Anyone can help me to fix it?

Thanks

Yann

#!/bin/env perl use Getopt::Std; use threads; use threads::shared; use Time::HiRes qw( usleep ); use POSIX (); # POSIX unmasks the sigprocmask properly my $sigset = POSIX::SigSet->new(); my $action = POSIX::SigAction->new('stopInt', $sigset, &POSIX::SA_NODEFER); POSIX::sigaction(&POSIX::SIGINT, $action); my %options; &getopts("t:c:h",\%options) || &say_usage; &say_usage if (!defined($options{c})); my $threadNb = 5; my $cmd = $options{c}; my $count : shared = 0; my $run : shared = 1; $DB::single=1; for ($i = 0; $i<$threadNb; $i++) { $threads[$i] = threads->new(\&workToDo, $cmd); } for ($i = 0; $i<$threadNb; $i++) { $threads[$i]->join(); } print "nb exec command $cmd = $count\n"; sub workToDo() { my $cmd = shift; my $sem = shift; while (&getRun()) { usleep(1000); #my $pid = open(PTCMD, "$cmd 2>&1 |"); { lock($count); ++$count; } } print "end workToDo\n"; } sub getRun { lock($run); return $run; } sub stopInt { print "stopInt \n"; lock($run); if ($run) { print "stop received\n"; $run = 0; } } sub say_usage { print "command MT - outil de lancement d'une commande shell en Mult +i thread\n"; print "\n\t$0 ...\n"; print << 'EOT'; Options: -c : shell command [-t] : thread number (default: nb Thread = 5) [-h] : help : show the help EOT exit; }

In reply to double free in a multithread script when signal is received by yann22

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.