Created
December 10, 2020 11:57
-
-
Save marcandre/8fc63e18a5be0da6a1d9e4c436533726 to your computer and use it in GitHub Desktop.
Timeout::wake
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def Timeout.wake(secs) | |
Thread.handle_interrupt(Timeout::Error => :on_blocking) do | |
Timeout.timeout(secs) do | |
yield | |
end | |
end | |
end | |
N = 100 | |
SECS = 0.1 | |
100.times do | |
t = Time.now | |
q = Queue.new | |
q2 = Queue.new | |
r = N.times.map do | |
Thread.new do | |
begin | |
q2 << Timeout.wake(SECS) do | |
q.pop | |
end | |
rescue Timeout::Error | |
:timeout | |
end | |
end | |
end | |
t2 = Thread.new { N.times { Thread.pass }} | |
sleep [t + SECS*0.95 - Time.now, 0].max | |
N.times { q << 42; Thread.pass } | |
r.each(&:join) | |
puts q2.size + q.size == N ? 'ok' : [q2.size, q.size] | |
end | |
# On my machine I get mostly "ok" but sometimes there's data missing. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment