45 lines
1.1 KiB
Plaintext
45 lines
1.1 KiB
Plaintext
_Mutexes_ are synchronization primitives that allow to manage concurrency.
|
|
|
|
* _non recursive mutexes_ are targeted by this rule. They can be locked/unlocked only once. Any locking/unlocking sequence that contains two consecutive identical operations leads to an undefined behaviour.
|
|
* _recursive mutexes_ are not target by this rule. They can be locked several times and unlocked several times as long as the number of locks/unlocks is the same.
|
|
|
|
This rule raises an issue when a ``++pthread_mutex_t++`` is locked or unlocked several times in a row. We assume that all ``++pthread_mutex_t++`` are non-recursive (this is the most common case).
|
|
|
|
|
|
== Noncompliant Code Example
|
|
|
|
----
|
|
pthread_mutex_t mtx1;
|
|
|
|
void bad1(void)
|
|
{
|
|
pthread_mutex_lock(&mtx1);
|
|
pthread_mutex_lock(&mtx1);
|
|
}
|
|
|
|
void bad2(void)
|
|
{
|
|
pthread_mutex_unlock(&mtx1);
|
|
pthread_mutex_unlock(&mtx1);
|
|
}
|
|
----
|
|
|
|
|
|
== Compliant Solution
|
|
|
|
----
|
|
pthread_mutex_t mtx1;
|
|
|
|
void ok(void)
|
|
{
|
|
pthread_mutex_lock(&mtx1);
|
|
pthread_mutex_unlock(&mtx1);
|
|
}
|
|
----
|
|
|
|
|
|
== See
|
|
|
|
* https://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_mutex_destroy.html[The Open Group] pthread_mutex_init, pthread_mutex_destroy
|
|
|