Another problem is self-deadlock. Self-deadlock occurs when a
single thread attempts to lock a mutex twice: the second attempt will block
indefinitely. This can easily happen when the same resource is used at multiple
levels within an algorithm.
In particular, consider a class that attempts to provide a
threadsafe interface by synchronising all member function calls with a single
internal mutex. The mutex is locked at the beginning of every method, and
unlocked on method return. If that class now calls a member function from
within a member function, there will be a self-deadlock.
To counter this problem there is the concept of recursive mutex.
A recursive mutex will allow multiple locks from within a single thread to
succeed, though that thread must unlock the mutex as many times as it has
locked it. The disadvantage of a recursive mutex is a slight performance
decrease.
See also:
No comments:
Post a Comment