Unicorn with delicious cookie
Our website uses cookies to enhance your browsing experience.
Accept
to the top
>
>
>
V2640. MISRA. Thread objects, thread sy…
menu mobile close menu
Analyzer diagnostics
General Analysis (C++)
General Analysis (C#)
General Analysis (Java)
Micro-Optimizations (C++)
Diagnosis of 64-bit errors (Viva64, C++)
Customer specific requests (C++)
MISRA errors
AUTOSAR errors
OWASP errors (C++)
OWASP errors (C#)
OWASP errors (Java)
Problems related to code analyzer
Additional information
toggle menu Contents

V2640. MISRA. Thread objects, thread synchronization objects and thread-specific storage pointers should have appropriate storage duration.

Apr 04 2025

This diagnostic rule is based on the MISRA (Motor Industry Software Reliability Association) software development guidelines.

This diagnostic rule is relevant only for C.

Objects of the cnd_t, mtx_t, thrd_t, and tss_t types from the Concurrency support library should not have automatic or thread-local storage duration.

Synchronization objects are typically accessed from multiple threads. In such cases, when using automatic or thread-local storage duration, developers risk accessing an object whose lifetime has already ended. This can lead to undefined behavior and uncontrolled thread execution.

The diagnostic rule helps reduce the likelihood of errors and eliminate thread dependencies on the lifetime of synchronization objects.

The example of the incorrect code:

int Task1(void *mtx)
{
  mtx_lock((mtx_t*)mtx);   // can be dangling pointer
  // do stuff
  mtx_unlock((mtx_t*)mtx); // can be dangling pointer
  return 0;
}

void RunTask()
{
  thrd_t thread1;
  mtx_t mtx;

  mtx_init(&mtx, mtx_plain);
  thrd_create(&thread1, Task1, &mtx);
}

In this case, the lifetime of the mtx object may end before it is locked or unlocked in the child thread.

The fixed version of this code:

thrd_t thread1;
mtx_t mtx;

int Task1(void *mtx)
{
  mtx_lock((mtx_t*)mtx); 
  // do stuff
  mtx_unlock((mtx_t*)mtx); 
  return 0;
}

void RunTask()
{
  mtx_init(&mtx, mtx_plain);
  thrd_create(&thread1, Task1, &mtx);
}

This diagnostic is classified as:

  • MISRA-C-22.13
close form

Fill out the form in 2 simple steps below:

Your contact information:

Step 1
Congratulations! This is your promo code!

Desired license type:

Step 2
Team license
Enterprise license
** By clicking this button you agree to our Privacy Policy statement
close form
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
* By clicking this button you agree to our Privacy Policy statement

close form
Free PVS‑Studio license for Microsoft MVP specialists
* By clicking this button you agree to our Privacy Policy statement

close form
To get the licence for your open-source project, please fill out this form
* By clicking this button you agree to our Privacy Policy statement

close form
I want to join the test
* By clicking this button you agree to our Privacy Policy statement

close form
check circle
Message submitted.

Your message has been sent. We will email you at


If you do not see the email in your inbox, please check if it is filtered to one of the following folders:

  • Promotion
  • Updates
  • Spam