Skip to content

Instantly share code, notes, and snippets.

@nahi
Created December 19, 2011 07:23
Show Gist options
  • Save nahi/1495873 to your computer and use it in GitHub Desktop.
Save nahi/1495873 to your computer and use it in GitHub Desktop.
require 'thread'
class Foo
attr_reader :a
attr_reader :assignment_count, :evaluation_count
def initialize
@a = nil
@evaluation_lock = Mutex.new
@assignment_lock = Mutex.new
@evaluation_count = 0
@assignment_count = 0
end
def a=(arg)
Thread.pass
@a = arg
@assignment_lock.synchronize do
@assignment_count += 1
end
end
def b
@evaluation_lock.synchronize do
@evaluation_count += 1
end
1
end
end
5.times do
obj = Foo.new
1000.times.map {
Thread. new {
obj.a ||= obj.b
}
}.each(&:join)
p [obj.assignment_count, obj.evaluation_count]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment