Are Python instance variables thread-safe?

后端 未结 4 1549
北荒
北荒 2020-12-13 02:38

OK, check following codes first:

class DemoClass():

    def __init__(self):
        #### I really want to know if self.Counter is thread-safe. 
        self         


        
4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-13 02:52

    No, it is not thread safe - the two threads are essentially modifying the same variable simultaneously. And yes, the solution is one of the locking mechanisms in the threading module.

    BTW, self.Counter is an instance variable, not a class variable.

提交回复
热议问题