Skip to content

Instantly share code, notes, and snippets.

@jqr
Created February 21, 2018 17:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jqr/e352a33402149c56222c83f642fdbaec to your computer and use it in GitHub Desktop.
Save jqr/e352a33402149c56222c83f642fdbaec to your computer and use it in GitHub Desktop.
Easy to reason about linear overlapped execution of multiple threads to force contention for tests.
require "thread"
# Easy to reason about linear overlapped execution of multiple threads to
# force contention for tests.
#
# -------- Time ------->
# Thread.current 1 ---------------------------------- 4
# t2 2 ------- 3
m = Mutex.new
cv = ConditionVariable.new
m.synchronize do
puts "1."
t2 = Thread.new do
m.synchronize do
puts "2."
cv.signal
puts "3."
end
end
cv.wait(m)
puts "4."
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment