Appendices > Portable Thread Library > Mutexes > Locking and Unlocking a Mutex
 
Locking and Unlocking a Mutex
A mutex provides two member functions, lock and unlock, which are used to lock the mutex and then unlock the thread when the operation has been performed. The call to lock will be blocked until the thread that has acquired the lock calls unlock.
The portable thread library provides two types of mutexes, an unsafe mutex and a safe mutex. The difference between these two types of mutexes is due to performance issues. In addition, unlike safe mutexes, unsafe mutexes may not be locked more than once by the same thread. The safe mutex guarantees correct performance of the mutex even if the same thread locks the mutex several times.
Important: The member functions lock and unlock must be nested so that the resource is released only when the correct number of unlocks have been performed. Since the member functions lock and unlock of the unsafe mutex are inline, they provide the same performance as the native member functions on any given machine. It is important to emphasize this point, since the member functions lock and unlock can be called many times.
If the application cannot guarantee that it will not lock the mutex multiple times from the same thread, then a safe mutex is required. The safe mutex has the same behavior as the unsafe mutex but it is less efficient in terms of performance.

Version 6.3
Copyright © 2018, Rogue Wave Software, Inc. All Rights Reserved.