Threadsafing a mixin module without using self.new or initialize.
Project: Eventable - https://github.com/mikbe/eventable
I need to threadsafe access to an instance variable, an array. To do so
wrapped all methods that access the array in a mutex.synchronize block.
problem is I’m creating the mutex in each function using
||= Mutex.new which could cause collisions if two threads try to use a
method that creates the mutex simultaneously.
I’ve verified this could happen: https://gist.github.com/1031308
Since this is a mixin module I can’t use initialize to create the mutex
instance variable and I don’t want to use trapself.new because if the
uses this method itself then the instance variable will never be
aversion to using self.new to create the instance variable is this: if I
redefine a method to fix an issue it’s not unreasonable for someone else
do the same thing. If their doing exactly what I did breaks what I did
it’s a bad fix. (see https://github.com/mikbe/eventable/pull/3)
So, my current idea for a solution is to create a class level mutex for
inheriting class that locks on the creation of the mutex, for instance:
https://gist.github.com/1031419. This seems ugly though and I’m fairly
to Ruby so I’m wondering if I’ve missed, or simply don’t know about,
something that would be a better solution.