MISRA C:2012 Rule 22.12
Thread objects, thread synchronization objects, and thread-specific storage pointers shall only be accessed by the appropriate Standard Library functions
Since R2025b
Description
Rule Definition
Thread objects, thread synchronization objects, and thread-specific storage pointers shall only be accessed by the appropriate Standard Library functions.1
Rationale
The Standard Library provides designated functions for accessing thread objects, thread synchronization objects and thread-specific storage pointers. Direct manipulation of these objects without using the designated Standard Library functions can lead to situations where the behavior is undefined.
For instance:
Functions such as
thrd_create
andthrd_join
ensure that a thread object is unique for a given thread. Likewise, functions such asmtx_lock
andmtx_unlock
ensure that a thread synchronization object is unique for a given mutex. Direct copy or assignment of these objects violate the uniqueness and can lead to undefined behavior.To implement thread-specific storage, you create a new thread-specific storage key using the Standard Library function
tss_create
and then associate memory with this key using functions such astss_set
. If you directly manipulate the key in between the creation and the usage, the behavior is undefined.
Polyspace Implementation
The rule checker reports a violation if:
Thread objects or objects of type
thrd_t
(declared inthreads.h
) are manipulated without usingthrd_create
,thrd_join
, or similar Standard Library functions.Mutex objects or objects of type
mtx_t
(declared inthreads.h
) are manipulated without usingmtx_lock
,mtx_unlock
, or similar Standard Library functions.Condition objects or objects of type
cnd_t
(declared inthreads.h
) are manipulated without usingcnd_init
,cnd_signal
, or similar Standard Library functions.Thread-specific storage keys or objects of type
tss_t
(declared inthreads.h
) are manipulated without usingtss_get
,tss_set
, or similar Standard Library functions.
The checker does not flag the uses of these objects with the sizeof
or alignof
operators.
Troubleshooting
If you expect a rule violation but Polyspace® does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
Group: Resources |
Category: Mandatory |
AGC Category: Mandatory |
Version History
Introduced in R2025b
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.