Main Content

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 and thrd_join ensure that a thread object is unique for a given thread. Likewise, functions such as mtx_lock and mtx_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 as tss_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 in threads.h) are manipulated without using thrd_create, thrd_join, or similar Standard Library functions.

  • Mutex objects or objects of type mtx_t (declared in threads.h) are manipulated without using mtx_lock, mtx_unlock, or similar Standard Library functions.

  • Condition objects or objects of type cnd_t (declared in threads.h) are manipulated without using cnd_init, cnd_signal, or similar Standard Library functions.

  • Thread-specific storage keys or objects of type tss_t (declared in threads.h) are manipulated without using tss_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

expand all

In this example, the function manipulateThreads() illustrates various direct manipulations of thrd_t objects that violate the rule. The function accessThreadsUsingLibraryFunctions() shows thread object accesses that are compliant with the rule.


#include <threads.h>
thrd_t  thread1;
thrd_t  thread2;

int startFunc(void * data);
void thrd_unknown(thrd_t);

void manipulateThreads() {
  thrd_t* thp = &thread1;                         // Noncompliant
  thread1 = thread2;                              // Noncompliant
  thread2 = 0;                                    // Noncompliant
  if (thread1) {                                  // Noncompliant
    memset(&thread1, sizeof(thrd_t), 0);          // Noncompliant
  }
  if (thread1 == thread2) {                       // Noncompliant
    thrd_unknown(thread2);                        // Noncompliant
  }
}

void accessThreadsUsingLibraryFunctions() {
  if (thrd_create( &thread1, startFunc, "Thread data" )) {  // Compliant
    thrd_join(thread2, 0);                                  // Compliant   
  }
  if (thrd_equal(thread1, thread2)) {                       // Compliant    
    thrd_detach(thread1);                                   // Compliant 
  }
}

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.