Sha*_*ane 20 python multithreading thread-safety
好的,首先检查以下代码:
class DemoClass():
def __init__(self):
#### I really want to know if self.Counter is thread-safe.
self.Counter = 0
def Increase(self):
self.Counter = self.Counter + 1
def Decrease(self):
self.Counter = self.Counter - 1
def DoThis(self):
while True:
Do something
if A happens:
self.Increase()
else:
self.Decrease()
time.sleep(randomSecs)
def DoThat(self):
while True:
Do other things
if B happens:
self.Increase()
else:
self.Decrease()
time.sleep(randomSecs)
def ThreadSafeOrNot(self):
InterestingThreadA = threading.Thread(target = self.DoThis, args = ())
InterestingThreadA.start()
InterestingThreadB = threading.Thread(target = self.DoThat, args = ())
InterestingThreadB.start()
Run Code Online (Sandbox Code Playgroud)
我面临着和上面相同的情况.我真的想知道它是否是线程安全的self.Counter,如果没有,我有什么选择?我只能想到threading.RLock()锁定这个资源,还有什么好主意?
Aar*_*lla 25
使用实例字段self.Counter是线程安全的或"原子的".阅读,或指定一个单值-即使它需要在内存中4个字节,你将永远不会得到一个半改变值.但是操作self.Counter = self.Counter + 1不是因为它读取值然后写入它 - 另一个线程可以在读取之后和写回之前更改字段的值.
所以你需要用锁来保护整个操作.
由于方法体基本上是整个操作,因此可以使用装饰器来完成此操作.请参阅此答案以获取示例:https://stackoverflow.com/a/490090/34088
Eli*_*sky 10
不,它不是线程安全的 - 两个线程实际上是同时修改同一个变量.是的,解决方案是threading模块中的锁定机制之一.
BTW,self.Counter是一个实例变量,不是类变量.