Skip to content

Instantly share code, notes, and snippets.

@marcandre
Created December 10, 2020 11:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcandre/8fc63e18a5be0da6a1d9e4c436533726 to your computer and use it in GitHub Desktop.
Save marcandre/8fc63e18a5be0da6a1d9e4c436533726 to your computer and use it in GitHub Desktop.
Timeout::wake
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