2011-08-27 19 views
0

我有以下的僞代碼:我如何可以鎖定整個班級

class A: 
    mutex lockForB 

class B: 
    def __init__(self, A): //the type of A is class A 
     lock(A.lockForB) 
     # ... 
     unlock(A.lockForB) 

    # other function have the same locking 

我但從接力點的理解是非常糟糕的主意,使用這樣的設計,但如果我創造一流的BI內鎖不會能夠鎖定B級的創建者。有沒有更好的設計?提前致謝。

回答

3

我不知道你要完成什麼。這不太可能是您想要的類級鎖,但您的僞代碼與實際代碼距離不太遠,所以我只需填入空格。老實說,沒有一些想法你試圖同步訪問它將是一個挑戰,以幫助你。

class A: 
    lockForB = threading.RLock() 

class B: 
    def __init__(self): 
     with A.lockForB: 
      # do init stuff 

    def othermethod(self): 
     with A.lockForB: 
      # do other stuff 

因此,代碼將起作用。 lockForB只是A上的一個類級屬性,因此它在A的所有實例之間共享。但是,在我見過人們使用類級鎖的情況下,通常會阻止擁有該鎖的類被放入一個不一致的狀態,你有兩個看似無關的類共享一個鎖。

沒有上下文來幫助理解你嘗試它真的很難告訴你爲什麼不能這樣寫同步訪問什麼:

class C: 
    lock = threading.RLock() 
    def __init__(self): 
     with self.lock: 
      # do stuff 
    def othermethod(self): 
     with self.lock: 
      # do other stuff 
1

一般來說,你應該把鎖只在周圍關鍵部分的界限或共享資源的使用。你應該考慮你試圖防止同時訪問和保護它。舉例來說,如果A類有一個隊列放入並讀取項目,那麼它就是對你應該保護的這個特定資源的訪問。由於OOP決定了這種資源應該由類的方法來訪問,只有A類應該保護:

class A(object): 
    def __init__(self, *args, **kws): 
     # do the initialization 
     self._my_queue = Queue() 
     self._lock = Lock() 

    def do_something(self): 
     # do initial calculations 
     self._lock.acquire() 
     item = self._my_queue.get() 
     self._lock.release() 
     # do other things 

因此提出,B類應調用類A方法,這將是線程安全的。如果B類都有自己的臨界區這是相當正常的使用比單鎖更多:

class B(object): 
    def __init__(self, *args, **kws): 
     # do the initialization 
     self._lock = Lock() 
     self.a = A() 

    def do_something_with_a(self): 
     # initial calculations 
     self._lock.acquire() 
     # Critical section 
     result = self.a.do_something() 
     # do something with result 
     self._lock.release() 
     # continue the code 

這樣每個類保護自己的關鍵部分,並共享資源,而且也沒有必要打破類的接口。

如果您需要保護類的C'tor,那麼您需要模塊的全局鎖,在類的範圍之外創建並初始化,或者將鎖添加到Class對象(如靜態成員C++和Java),而不是實例本身:

class B(object): 
    def __init__(self, *args, **kws): 
     if not hasattr(self.__class__, "_lock"): 
      self.__class__._lock = Lock() 
     # works with Python 2.6+ for earlier version use try-finally 
     with self.__class__._lock: 
      # Your initialization 

這將保護您的C'tor

相關問題