Main Content

CERT C: Rec. CON07-C

R2026b

Ensure that compound operations on shared variables are atomic

Since R2026b

Description

Ensure that compound operations on shared variables are atomic1

Polyspace Implementation

Polyspace® checks for the issue Data race.

Examples

expand all

Issue

This issue occurs when these events occur in sequence:

  1. Multiple tasks perform unprotected operations on a shared variable.

  2. At least one task performs a write operation.

Risk

A data race can result in unpredictable values of the shared variable because you do not control the order of the operations in different tasks.

Data races between two write operations are more serious than data races between a write and read operation. Two write operations can interfere with each other and result in indeterminate values. To identify write-write conflicts, use the filters on the Detail column of the Results List pane. For these conflicts, the Detail column shows the additional line:

 Variable value may be altered by write-write concurrent access.
See also Filter and Sort Results in Polyspace Platform User Interface or Filter and Sort Results in Polyspace Access Web Interface (Polyspace Access).

Fix

To fix this defect, protect the operations on the shared variable using critical sections, temporal exclusion, or another means. See Protections for Shared Variables in Multitasking Code.

To identify existing protections that you can reuse, see the table and graphs associated with the result in Polyspace Platform user interface. The table shows each pair of conflicting calls. The Access Protections column shows existing protections on the calls. To see the function call sequence leading to the conflicts, click the icon.

Example — Non-Atomic Increment of Shared Variable

In this example, Polyspace detects that toggle_flag() performs a nonatomic read-modify-write on a shared variable.

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

static bool flag = false;

void toggle_flag(void) {
    flag = !flag;  // Noncompliant
}

int worker(void *arg) {
    toggle_flag();
    return 0;
}

int main(void) {
    thrd_t t1, t2;
    thrd_create(&t1, worker, NULL);
    thrd_create(&t2, worker, NULL);
    thrd_join(t1, NULL);
    thrd_join(t2, NULL);
    return 0;
}

Multiple threads read and write the shared nonatomic variable flag without synchronization. The operation flag = !flag is a compound read-modify-write that is not atomic.

Correction — Use Atomic Compare-and-Exchange

Use atomic_compare_exchange_weak() to perform the toggle atomically without a mutex.

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

static atomic_bool flag;

void init_flag(void) {
    atomic_init(&flag, false);
}

void toggle_flag(void) {
    bool old_flag = atomic_load(&flag);
    bool new_flag;
    do {
        new_flag = !old_flag;
    } while (!atomic_compare_exchange_weak(&flag, &old_flag, new_flag));  // Compliant
}

int worker(void *arg) {
    toggle_flag();
    return 0;
}

int main(void) {
    init_flag();
    thrd_t t1, t2;
    thrd_create(&t1, worker, NULL);
    thrd_create(&t2, worker, NULL);
    thrd_join(t1, NULL);
    thrd_join(t2, NULL);
    return 0;
}

Check Information

Group: Rec. 14. Concurrency (CON)
PQL Name: std.cert.CON07_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.