If you want to lock methods on instances, you have to create a Mutex
for every instance and method:
class Foo
def initialize
@blue_mutex = Mutex.new
@red_mutex = Mutex.new
end
def blue
@blue || @blue_mutex.synchronize do
@blue ||= Blue.new
end
end
def red_mutex
@red || @red_mutex.synchronize do
@red ||= Red.new
end
end
end
Is there a logical error with having Thread.exclusive
take arguments?
class Foo
def blue
@blue || Thread.exclusive("#{object_id}/blue") do
@blue ||= Blue.new
end
end
def red
@red || Thread.exclusive("#{object_id}/red") do
@red ||= Red.new
end
end
end
Why create mutexes if Thread.exclusive
could just take an argument that defines the scope of the exclusivity?