Define alarm in threads

  • Thread starter Sébastien Cottalorda
  • Start date
S

Sébastien Cottalorda

I need to define an alarm like this.

eval {
my $thr = threads->new(\&run_cmd, params);
};
if ($@){
print "$@\n";
}
blah blah blah


sub run_cmd {
blah blah blah
threads->detach();
eval {
local $SIG{ALRM} = sub { die "TIMEOUT\n" };
alarm 10;
`$command_to_execute`;
alarm 0;
};
if ($@){
print "$@\n";
}
}

My problem is the following :
after waiting 10 seconds, either the master or the thread die.

How can I timeout the thread without affecting the master ?

Thanks in advance for any kind of help.

Sebastien
 
D

Dr.Ruud

Sébastien Cottalorda said:
eval {
my $thr = threads->new(\&run_cmd, params);
};
if ($@){
print "$@\n";
}

You are not using $thr.

Below an alternative that uses the return value of eval
(in stead of relying on the global $@)

eval {
threads->new(\&run_cmd, @params);
1; # success
}
or do {
my $err = $@ || "unknown";
print "$err\n";
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top