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 as You Code).
Rule Definition
Thread synchronization objects and thread-specific storage pointers shall not be destroyed until after all threads accessing them have terminated1 .
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 objectmyMutexand the threadfoo()destroys it. The mutex objectmyMutexcan 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,myStoragemight 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.
A task deletes a thread specific storage object before all thread using it are joined.
A task deletes thread specific storage object after it is registered using
tss_create()but before any threads using the object are started.
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).
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 |
PQL Name: std.misra_c_2012.R22_15 |
Version History
Introduced in R2024bSee Also
1 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.
The MISRA coding standards referenced in the Polyspace Bug Finder™ documentation are from the following MISRA standards:
MISRA C:2004
MISRA C:2012
MISRA C:2023
MISRA C++:2008
MISRA C++:2023
MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.