Skip to content

Instantly share code, notes, and snippets.

@headius
Forked from nahi/gist:1495459
Created December 19, 2011 06:17
Show Gist options
  • Save headius/1495677 to your computer and use it in GitHub Desktop.
Save headius/1495677 to your computer and use it in GitHub Desktop.
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