Main Content

CERT C: Rec. CON06-C

R2026b

Ensure that every mutex outlives the data it protects

Since R2026b

Description

Ensure that every mutex outlives the data it protects1 .

Polyspace Implementation

Polyspace® checks for the issue Use of local mutex.

Examples

expand all

Issue

The issue occurs when a local instance of a mutex object is used for protecting shared resources.

Risk

Using a local mutex to protect shared data leaves the shared data unprotected. Consider this code:

int shared_counter = 0;

void increment(void) {
    mtx_t lock;
    mtx_init(&lock, mtx_plain);

    mtx_lock(&lock);
    shared_counter++;
    mtx_unlock(&lock);

    mtx_destroy(&lock);
}
When multiple threads call the function increment(), each instance of the function locks its own local mutex. Because the mutexes are local, the synchronization between two threads necessary to safely access shared_counter does not occur. Concurrent access to the same shared data can occur, leading to data races and undefined behavior.

Fix

Use a global mutex object that outlives the protected data. For static data, use a static lock and acquire that lock when accessing the shared data.

Example

In this example, each thread receives its own mutex through a struct instance. Because the locks are per-instance, they do not synchronize access to the shared static counter between threads.

#include <threads.h>
#include <stddef.h>

typedef struct {
    mtx_t lock;
} CountBoxes;

static int counter = 0;

int increment(void *arg) {
    CountBoxes *self = (CountBoxes *)arg;
    mtx_lock(&self->lock); // Noncompliant
    counter++;
    mtx_unlock(&self->lock);
    return 0;
}

int main(void) {
    CountBoxes obj_a, obj_b;
    mtx_init(&obj_a.lock, mtx_plain);
    mtx_init(&obj_b.lock, mtx_plain);

    thrd_t t1, t2;
    thrd_create(&t1, increment, &obj_a);
    thrd_create(&t2, increment, &obj_b);
    thrd_join(t1, NULL);
    thrd_join(t2, NULL);

    mtx_destroy(&obj_a.lock);
    mtx_destroy(&obj_b.lock);
    return 0;
}
Correction

Use a shared static mutex that outlives the data it protects. All threads lock the same mutex before accessing the shared counter.

#include <threads.h>
#include <stddef.h>

static int counter = 0;
static mtx_t lock; // Compliant

int increment(void *arg) {
    (void)arg;
    mtx_lock(&lock); // Compliant
    counter++;
    mtx_unlock(&lock);
    return 0;
}

int main(void) {
    mtx_init(&lock, mtx_plain);

    thrd_t t1, t2;
    thrd_create(&t1, increment, NULL);
    thrd_create(&t2, increment, NULL);
    thrd_join(t1, NULL);
    thrd_join(t2, NULL);

    mtx_destroy(&lock);
    return 0;
}

Check Information

Group: Rec. 14. Concurrency (CON)
PQL Name: std.cert.CON06_C

Version History

Introduced in R2026b


1 This software has been created by MathWorks incorporating portions of: the “SEI CERT-C Website,” © 2017 Carnegie Mellon University, the SEI CERT-C++ Web site © 2017 Carnegie Mellon University, ”SEI CERT C Coding Standard – Rules for Developing safe, Reliable and Secure systems – 2016 Edition,” © 2016 Carnegie Mellon University, and “SEI CERT C++ Coding Standard – Rules for Developing safe, Reliable and Secure systems in C++ – 2016 Edition” © 2016 Carnegie Mellon University, with special permission from its Software Engineering Institute.

ANY MATERIAL OF CARNEGIE MELLON UNIVERSITY AND/OR ITS SOFTWARE ENGINEERING INSTITUTE CONTAINED HEREIN IS FURNISHED ON AN "AS-IS" BASIS. CARNEGIE MELLON UNIVERSITY MAKES NO WARRANTIES OF ANY KIND, EITHER EXPRESSED OR IMPLIED, AS TO ANY MATTER INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR PURPOSE OR MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE OF THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT MAKE ANY WARRANTY OF ANY KIND WITH RESPECT TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT INFRINGEMENT.

This software and associated documentation has not been reviewed nor is it endorsed by Carnegie Mellon University or its Software Engineering Institute.