Created
December 7, 2012 17:29
-
-
Save louismullie/4234875 to your computer and use it in GitHub Desktop.
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
require 'thread' | |
$done = {} | |
$size = 2 | |
$mutex = Mutex.new | |
$resource = ConditionVariable.new | |
def register(thread) | |
id = thread.object_id | |
$mutex.synchronize do | |
$resource.wait($mutex) | |
$done[id] = false | |
$resource.broadcast($mutex) | |
end | |
while $done.size != $size | |
sleep 0.001 | |
end | |
end | |
def done(thread) | |
id = thread.object_id | |
$mutex.synchronize do | |
$resource.wait($mutex) | |
$done[id] = true | |
$resource.broadcast($mutex) | |
end | |
end | |
def syncthreads | |
until $done.all? { |x| x[1] == true} | |
print $done | |
sleep 0.00001 | |
end | |
$done.each do |k,v| | |
$done[k] = false | |
end | |
end | |
x = 0 | |
a = Thread.new do | |
register(Thread.current) | |
x = 1 | |
done(Thread.current) | |
syncthreads() | |
end | |
b = Thread.new do | |
register(Thread.current) | |
# do nothing | |
done(Thread.current) | |
syncthreads() | |
if x == 0 | |
raise "Lol" | |
end | |
end | |
x = 0 | |
[a,b].each { |t| t.join } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment