Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. Skip to content. Change Language. Related Articles. Table of Contents. Save Article. Improve Article. Like Article. Previous Conditional wait and signal in multi-threading. Next Mutex vs Semaphore. Recommended Articles. Article Contributed By :. Easy Normal Medium Hard Expert. Writing code in comment?
Please use ide. Load Comments. What's New. Mutex objects are intended to serve as a low-level primitive from which other thread synchronization functions can be built. As such, the implementation of mutexes should be as efficient as possible, and this has ramifications on the features available at the interface. The mutex functions and the particular default settings of the mutex attributes have been motivated by the desire to not preclude fast, inlined implementations of mutex locking and unlocking.
For example, on systems not supporting the XSI extended mutex types, deadlocking on a double-lock is explicitly allowed behavior in order to avoid requiring more overhead in the basic mechanism than is absolutely necessary. More "friendly" mutexes that detect deadlock or that allow multiple locking by the same thread are easily constructed by the user via the other mechanisms provided.
Implementations might also choose to provide such extended features as options via special mutex attributes. Since most attributes only need to be checked when a thread is going to be blocked, the use of attributes does not slow the common mutex-locking case. To remove a node from the list, first search the list starting at ListHead which itself is never removed until the desired node is found. To protect this search from the effects of concurrent deletions, lock each node before any of its contents are accessed.
Because all searches start at ListHead , there is never a deadlock because the locks are always taken in list order. When the desired node is found, lock both the node and its predecessor since the change involves both nodes. Because the predecessor's lock is always taken first, you are again protected from deadlock.
Example shows the C code to remove an item from a singly linked list. Example modifies the previous list structure by converting it into a circular list.
There is no longer a distinguished head node; now a thread might be associated with a particular node and might perform operations on that node and its neighbor. Note that lock hierarchies do not work easily here because the obvious hierarchy following the links is circular. Here is the C code that acquires the locks on two nodes and performs an operation involving both of them.
Mutex Lock Code Examples Example shows some code fragments with mutex locking.
0コメント