JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.

If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.

If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.

CodeSonar® 9.2p0 CONFIDENTIAL CodeSecure Inc
C and C++


CONCURRENCY.LOCK.ORDER : Conflicting Lock Order

Summary

Different threads acquire the same locks in different orders. This can lead to deadlock or other undefined behavior.

Properties

Class Name Conflicting Lock Order
Significance reliability
Mnemonic CONCURRENCY.LOCK.ORDER
Categories
MisraC2025 MisraC2025:22.16 All mutex objects locked by a thread shall be explicitly unlocked by the same thread
MisraC2023 MisraC2023:22.16 All mutex objects locked by a thread shall be explicitly unlocked by the same thread
Misra2012 Misra2012:22.16 All mutex objects locked by a thread shall be explicitly unlocked by the same thread
CWE CWE:413 Improper Resource Locking
  CWE:696 Incorrect Behavior Order
CERT-C CERT-C:CON35-C Avoid deadlock by locking in a predefined order
  CERT-C:POS51-C Avoid deadlock with POSIX threads by locking in predefined order
CERT-CPP CERT-CPP:CON53-CPP Avoid deadlock by locking in a predefined order
JPL JPL:9 Place restrictions on the use of semaphores and locks.
Availability Available for C and C++.
Enabling Checks for this warning class are disabled by default. To enable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += allow class="Conflicting Lock Order"

Triggering Functions

Conflicting Lock Order warnings can be triggered by the following functions.

Example

#include <pthread.h>

pthread_mutex_t Green;
pthread_mutex_t Blue;


/* ----- Thread 1 ----- */
void green_before_blue(void){
    pthread_mutex_lock(&Green);                        /* Green acquired before Blue */
    pthread_mutex_lock(&Blue);  /* 'Conflicting Lock Order' warning issued either here or in marked location below */
    /* ... operate on locked data */
    pthread_mutex_unlock(&Blue);
    pthread_mutex_unlock(&Green);
}

/* ----- Thread 2 ----- */
void blue_before_green(void){
    pthread_mutex_lock(&Blue);                         /* Blue acquired before Green */
    pthread_mutex_lock(&Green);  /* 'Conflicting Lock Order' warning issued either here or in marked location above */
    /* ... operate on locked data */
    pthread_mutex_unlock(&Green);
    pthread_mutex_unlock(&Blue);
}

In a multithreaded environment, two threads could try to obtain the two locks in different orders, possibly resulting in deadlock:

Conflicting Lock Order example diagram

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.

 

To report problems with this documentation, please visit https://support.codesecure.com/.