Reputation: 77
what is the difference between using mutex as a global variable or as an instance variable? for example, these two usages?:
my_lock = RLock()
class myclass:
def __init__(self):
self.mutex = my_lock
def func():
with self.mutex:
#do something
and
class myclass:
def __init__(self):
self.mutex = RLock()
def func():
with self.mutex:
#do something
Upvotes: 0
Views: 167
Reputation: 181027
Your first version;
my_lock = RLock()
class myclass:
def __init__(self):
self.mutex = my_lock
...creates a single mutex common to all instances of the class, while
class myclass:
def __init__(self):
self.mutex = RLock()
...creates a mutex per instance.
In the first case, the mutex blocks func
from executing simultaneously in any myclass object.
In the second case, the mutex blocks func
from executing simultaneously in a single myclass object.
Upvotes: 1