yann22 has asked for the wisdom of the Perl Monks concerning the following question:
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; }
|
|---|