class method
Thread.pending_interrupt?
(*untyped args) -> bool
Thread.pending_interrupt?(error = nil) → true/false
thread.c
View on GitHub ↗
static VALUE
rb_thread_s_pending_interrupt_p(int argc, VALUE *argv, VALUE self)
{
return rb_thread_pending_interrupt_p(argc, argv, GET_THREAD()->self);
}
Returns whether or not the asynchronous queue is empty.
Since Thread::handle_interrupt can be used to defer asynchronous events, this method can be used to determine if there are any deferred events.
If you find this method returns true, then you may finish :never blocks.
For example, the following method processes deferred asynchronous events immediately.
Example
def Thread.kick_interrupt_immediately Thread.handle_interrupt(Object => :immediate) { Thread.pass } end
If error is given, then check only for error type deferred events.
Usage
th = Thread.new{
Thread.handle_interrupt(RuntimeError => :on_blocking){
while true
...
# reach safe point to invoke interrupt
if Thread.pending_interrupt?
Thread.handle_interrupt(Object => :immediate){}
end
...
end
}
}
...
th.raise # stop thread
This example can also be written as the following, which you should use to avoid asynchronous interrupts.
flag = true
th = Thread.new{
Thread.handle_interrupt(RuntimeError => :on_blocking){
while true
...
# reach safe point to invoke interrupt
break if flag == false
...
end
}
}
...
flag = false # stop thread
See also
#pending_interrupt?
.pass
.report_on_exception
.new
also in
4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0