develooper Front page | perl.ithreads | Postings from September 2011

Re: threads and alarm

Thread Previous | Thread Next
From:
Jerry D. Hedden
Date:
September 1, 2011 05:48
Subject:
Re: threads and alarm
Message ID:
CABcAEYwNDrVuVLUpxK3mZEGj-0=UApH9w3oUsPK16=XMtLn7=g@mail.gmail.com
Signals are received by thread 0 - the main thread.  Therefore, when a
thread sets an alarm, the ALRM signal hits the main thread not the
thread that set the alarm.  I don't know your particular needs, but if
you want the thread to get the alarm, then you need to set the alarm
handler in the main thread, and then send the ALRM signal to the
thread using $thr->kill('ALRM').  Thus, in your example, if you add
the following:

$thr->detach();
$SIG{ALRM} = sub { $thr->kill('ALRM') };

It'll work.

On Thu, Sep 1, 2011 at 06:08, alfonso caponi <alfonso.caponi@gmail.com> wrote:
> Hi list,
>
> I need help! :) How can I use "alarm" without exit from a thread only
> (without making the script end!).
>
> For example this works (without threads):
>
> while(1){
>         eval {
>                 local $SIG{ALRM} = sub { print "alarm!\n"; };
>                 alarm(2);
>                 print "hi\n";
>                 sleep 5;
>                 alarm(0);
>         };
> }
>
> but the script in attachment doesn't works :(
>
> Thank you very much,
> Al
>

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About