Thread.pending_interrupt?(error = nil) â true/false
Class Public methods
Returns whether or not the asynchronous queue is empty.
Since ::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.
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 threadThis 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
Please login to continue.