MISRA C:2012 Rule 22.15
Thread synchronization objects and thread-specific storage pointers shall not be destroyed until after all threads accessing them have terminated
Since R2024b
Description
This checker is deactivated in a default Polyspace® as You Code analysis. See Checkers Deactivated in Polyspace as You Code Analysis (Polyspace Access).
Rule Definition
Thread synchronization objects and thread-specific storage pointers shall not be destroyed until after all threads accessing them have terminated.
This rule comes from MISRA C™: 2012 Amendment 4.
Rationale
Destroying mutex objects or thread-specific storage objects before the termination of all threads accessing these resources can lead to undefined behavior. For example:
The function
mtx_destroy()
releases the resources used by a mutex object. In this code, the threadbar()
locks the mutex objectmyMutex
and the threadfoo()
destroys it. The mutex objectmyMutex
can be locked when it is destroyed byfoo()
.Destroying a locked mutex results in undefined behavior.mtx_t myMutex; //... void foo() { // Thread foo //... mtx_destroy(myMutex); // Can be undefined behavior } void bar() { // Thread bar mtx_lock(myMutex); //... }
The function
tss_delete()
releases all resources used by a thread-specific storage object. In this code, the threadfoo()
accesses the thread specific storage objectmyStorage
. When the threadbar()
releases this storage,myStorage
might still be used by the threadfoo()
, resulting in undefined behavior.tss_t myStorage; //... void foo() { // Thread foo tss_set(myStorage, malloc(4)); //... } void bar() { // Thread bar //.. tss_delete(myStorage); // Can be undefined behavior }
The function
cnd_destroy()
destroys condition variables. In this code, the threadsfoo()
andbar()
use the condition variablemyCV
. When the threadbar()
destroysmyCV
, the condition variable might still be used by the threadfoo()
, resulting in undefined behavior.mtx_t myMutex; cnd_t myCV; //... void foo() { // Thread foo //... cnd_wait(&myCV, &myMutex); //... } void bar() { // Thread bar //.. cnd_wait(&myCV, &myMutex); //.. cnd_destroy(&cond_var); // Can be undefined behavior }
To avoid such instances of undefined behavior, check that all threads have been terminated before destroying shared resources. Alternatively, avoid destroying shared resources at all.
Polyspace Implementation
Polyspace reports a violation of this rule when a task destroys a mutex object after
it is locked and before it is unlocked. The locking and destruction can happen in the
same task or different tasks. A task is a function that you specify as an entry point
using the option Tasks (-entry-points)
.
On the Result Details pane, you see two events, the locking and destruction of the mutex, and the tasks that initiate the events. To navigate to the corresponding line in your source code, click the event.
Troubleshooting
If you expect a rule violation but do not see it, refer to Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
Group: Resources |
Category: Required |
AGC Category: Required |
Version History
Introduced in R2024b