MISRA C:2012 Rule 22.14
Thread synchronization objects shall be initialized before being accessed
Since R2025b
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 shall be initialized before being accessed.1
Rationale
If you do not initialize the thread synchronization objects before starting the threads, the threads might access these objects in an invalid state. Checking the validity of the synchronization objects in such code cannot be done deterministically.
Polyspace Implementation
Polyspace reports a violation of this rule if either of these conditions is true:
A
cnd_t
object is used in the code but it is not explicitly created usingcnd_init()
.A
mtx_t
object is used in the code but it is not explicitly created usingmtx_init()
.Multiple functions call the functions
cnd_init()
andmtx_init()
. Polyspace expects each these functions to be called from a unique initialization function.
Extend Checker
To extend this checker, in a Datalog file specify either or both:
A unique function that calls
cnd_init()
.A unique function that calls
mtx_init()
.
For example, this Datalog code designates foo()
as the function that
calls cnd_init()
and bar()
as the function that calls
mtx_init()
. Then, it instructs Polyspace to report a violation when either of these conditions are true:
A thread starts before the
bar()
.A thread starts before the
foo()
.
.include "models/interfaces/concurrency.dl" Concurrency.Basic.raise_if_thread_start_before_mtx_init(). Concurrency.Basic.raise_if_thread_start_before_cnd_init(). Concurrency.Basic.is_unique_function_cnd_init("foo"). Concurrency.Basic.is_unique_function_mutex_init("bar").
.dl
) file and use it as an input to the option -code-behavior-specifications
. For more details about modifying checker
behavior using Datalog, see Modify Bug Finder Checkers Through Code Behavior Specifications.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.