Unicorn with delicious cookie
Nous utilisons des cookies pour améliorer votre expérience de navigation. En savoir plus
Accepter
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.

04 Avr 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

Remplissez le formulaire ci‑dessous en 2 étapes simples :

Vos coordonnées :

Étape 1
Félicitations ! Voici votre code promo !

Type de licence souhaité :

Étape 2
Team license
Enterprise licence
** En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité
close form
Demandez des tarifs
Nouvelle licence
Renouvellement de licence
--Sélectionnez la devise--
USD
EUR
* En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité

close form
La licence PVS‑Studio gratuit pour les spécialistes Microsoft MVP
close form
Pour obtenir la licence de votre projet open source, s’il vous plait rempliez ce formulaire
* En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité

close form
I want to join the test
* En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité

close form
check circle
Votre message a été envoyé.

Nous vous répondrons à


Si l'e-mail n'apparaît pas dans votre boîte de réception, recherchez-le dans l'un des dossiers suivants:

  • Promotion
  • Notifications
  • Spam