Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c
memory

What is memory_order in C?

Dian Us Suqlain

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

We use memory_order as an argument in functions that require atomic operations to occur. It specifies how operations on distinct threads are synchronizedoccuring at the same time or rate.

The compiler or CPU can reorder memory access. They can execute in different orders when a separate thread tries to communicate, as well as observe a hierarchy of memory access that breaks the invariantsunchanging elements of the code.

To overcome this, we use the memory_order enumEnumeration (or enum) is a user-created data type that consists of integral constants., which specifies which types of reorderings a compiler must forbid.

memory_order determines the order of memory accesses for an atomic operation even for regular, non-atomic memory accesses.

Store and write operations on distinct memory_order constants

memory_order constants

The memory_order enum has multiple constants with specified usages.

  1. memory_order_relaxed: this type of atomic operation can have various optimizations performed on them, and they do not guarantee an order concerning locking and normal memory accesses.

  2. memory_order_acquire: this prevents ordinary loads and stores from being reordered before the atomic operation.

  3. memory_order_consume: this performs the same operation as memory_order_acquire, except that the ordering guarantees only apply to dependent data.

  4. memory_order_release: this prevents ordinary loads and stores from being reordered after the atomic operation.

  5. memory_order_acq_rel: this is a hybrid of memory_order_acquire and memory_order_release. Unlike the sequentially consistent model, this hybrid applies a happens-before relationship to dependent variables. This variant allows the synchronization requirements between independent readings and writes to be relaxed.

  6. memory_order_seq_cst: this operates when all access to memory (that may have visible side effects on the other threads involved) has already happened. This is the strictest memory order variant. Thus, it guarantees the most negligible unexpected side effects between the thread interactions through non-atomic memory accesses.

Note: memory_order operations in C are specified by including a built-in library <stdatomic.h> in the code file.

RELATED TAGS

c
memory

CONTRIBUTOR

Dian Us Suqlain
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring