-
-
Save headius/1495677 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
class Foo | |
def initialize | |
@var = 0 | |
# pad the variables apart to try to force them onto different cache lines | |
32.times do |i| | |
eval "@pad#{i} = nil" | |
end | |
@updated = false | |
end | |
def update | |
@updated = true | |
@var = 42 | |
end | |
def check | |
raise if @var == 42 && !@updated | |
@updated | |
end | |
end | |
100.times do | |
100.times do | |
obj = Foo.new | |
t1 = Thread.new { | |
sleep 0.01 | |
obj.update | |
} | |
100.times.map { | |
Thread.new { | |
Thread.pass while !obj.check | |
} | |
}.each(&:join) | |
t1.join | |
end | |
puts 'another done' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment