liveBook · Manning

Read-modify-write operation

As explained in Modern C, Third Edition

A read-modify-write operation atomically reads an object, computes or selects a new value, and writes that value as one indivisible operation.

A read-modify-write operation atomically reads an object, computes or selects a new value, and writes that value in one indivisible operation. In concurrent code, it can synchronize on the read side with one thread and on the write side with others. section 21.1

Atomic update versus assignment

With _Atomic(unsigned) x = 11;, ++x and x-- are read-modify-write operations. By contrast, x = 31 and x = 5 are writes, while unsigned y = x is a read. The atomic update combines its read and write into one operation. section 21.1

Recognized forms include:

  • Calls to atomic_exchange and atomic_compare_exchange_weak for _Atomic objects. section 21.1
  • Compound assignments, increment, and decrement operators for arithmetic _Atomic objects. section 21.1
  • Calls to atomic_flag_test_and_set for atomic_flag. section 21.1
Figure 21.1 Two threads that synchronize via an atomic. The circles present the modifications of object x. The bars below the threads represent information about the state of A, and those above represent information about the state of B.
figure

A two-thread diagram traces modifications of an atomic object and shows how read-modify-write operations participate in synchronization. section 21.1

How Modern C, Third Edition covers it

How deep it goes
Defines recognized forms and synchronization behavior, but stops short of implementation-level mechanics.
Examples
Uses atomic increment and decrement alongside separate reads and writes of an atomic unsigned object.
What it assumes
Assumes atomic objects, threads, and synchronization terminology while distinguishing syntax-level sequencing from synchronization.
What you can do after
Evaluate/understand which atomic expressions combine reading and writing, and how those expressions synchronize between threads.
How it connects
Connects atomic objects with acquire semantics, release semantics, synchronizes-with, and happened-before relations.

Also explained in

Compare how 3 books explain read-modify-write operation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C, Third Edition. The section links point to the parts of the book it draws on.