Using class instance variable for mutex in Ruby

前端 未结 2 1981
半阙折子戏
半阙折子戏 2021-01-19 13:31

Note: The code summary shown below is not a distillation of the code that I had the problem with. I\'ve left this original summary here since someo

相关标签:
2条回答
  • 2021-01-19 14:05

    Does it happen with a class variable? @@mutex. There might be a race condition with making new class-instances between threads and the new copy of @mutex isn't ready yet. Constants and class variables however, are shared between copies of the class and subclasses. Also, what if you put the @mutex init code in a memoized method such as:

    def self.mutex
      @mutex ||= Mutex.new
    end
    
    0 讨论(0)
  • 2021-01-19 14:12

    While autoloading is indeed not thread safe in Rails like it is in Ruby 1.9 (per Is autoload thread-safe in Ruby 1.9?), the problem I encountered was not due to that problem and the code I had was not an instance of the code I showed above, but rather an instance of the following:

    class Foo
      @mutex = Mutex.new
      def self.bar
         @mutex.synchronize { }
      end
    end
    
    class Foobar < Foo ; end
    
    Foobar.bar
    

    The problem is that when executing method from a superclass, the value of self remains unchanged, so the value of @mutex within Foo.bar is interpreted in the context of the Foobar object, not the value of the Foo object.

    This problem can be avoided by using a class variable (e.g. @@mutex) for the mutex.

    0 讨论(0)
提交回复
热议问题