Unicorn with delicious cookie
Our website uses cookies to enhance your browsing experience.
Accept
to the top
>
>
>
V2643. MISRA. All memory synchronizatio…
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

V2643. MISRA. All memory synchronization operation should be executed in sequentially consistent order.

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.

Using functions to handle atomic operations implies that the argument specifying the memory model type should always be set to memory_order::memory_order_seq_cst (sequentially consistent ordering).

When atomic operations are used in multithreaded code, one thread can access a variable value before another thread changes it. To synchronize read/write operations, C provides memory models defined in the memory_order enumeration.

The selected memory model can impact how the compiler and processor optimize the code. The default behavior is sequentially consistent ordering. This behavior pattern is the most intuitive for developers and the strictest model for compilers and processors. When it's used, read/write operations are performed exactly in the order in which they are written in the code.

This diagnostic rule is intended to minimize errors and exclude dependency of parallel code execution on processor and compiler optimizations.

The following functions from the Concurrency support library should be called with memory_order::memory_order_seq_cst:

  • atomic_load_explicit
  • atomic_store_explicit
  • atomic_flag_test_and_set_explicit
  • atomic_flag_clear_explicit
  • atomic_exchange_explicit
  • atomic_compare_exchange_strong_explicit
  • atomic_compare_exchange_weak_explicit
  • atomic_fetch_add_explicit
  • atomic_fetch_sub_explicit
  • atomic_fetch_or_explicit
  • atomic_fetch_xor_explicit
  • atomic_fetch_and_explicit
  • atomic_thread_fence
  • atomic_signal_fence.

In addition to explicitly passing the argument, developers can use non-*_explicit alternatives of these functions. In this case, the memory_order::memory_order_seq_cst argument will be implicitly passed.

The example of the incorrect code:

_Atomic int flag; // initialized elsewhere with non-zero value

void thread1()
{
  // do stuff
  atomic_store_explicit(&flag, 0, memory_order_release);
}

void thread2()
{
  auto value = atomic_load_explicit(&flag, memory_order_acquire);
  while (value != 0)
  {
    // do stuff
    value = atomic_load_explicit(&flag, memory_order_acquire);
  }
}

In the synthetic example above, the order of the load and store operations cannot be predicted because the non-strict memory model allows the compiler and processor to rearrange the operation order.

The fixed code:

_Atomic int flag; // initialized elsewhere with non-zero value

void thread1()
{
  // do some stuff

  atomic_store_explicit(&flag, 0, memory_order_seq_cst);
  // or use atomic_store(&flag, 0);
}

void thread2()
{
  auto value = atomic_load_explicit(&flag, memory_order_seq_cst);

  // or declaration via 'atomic_load'
  // auto value = atomic_load(&flag);

  while (value != 0)
  {
    // do some stuff

    value = atomic_load_explicit(&flag, memory_order_seq_cst);
    // or assignment via 'atomic_load'
    // value = atomic_load(&flag);
  }
}

This diagnostic is classified as:

  • MISRA-C-21.25
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