On this page
  1. atomic_fetch_add
  2. Include
  3. Signature & Overview
  4. Notes & Pitfalls
  5. Example

atomic_fetch_add

Prev: atomic_load | Up: stdatomic.h | Next: atomic_compare_exchange_weak
Rel: stdatomic.h, Synchronization & Coordination
Ref: cppreference — atomic_fetch_add
Tags: #public #prog #c #stdlib #stdatomic #tier/specialized


Include

#include <stdatomic.h>

Signature & Overview

T atomic_fetch_add(volatile A *obj, M arg);

Atomically adds arg to *obj and returns the previous value without a mutex.

Notes & Pitfalls

Example

#include <assert.h>
#include <stdatomic.h>

int main(void) {
    atomic_int next_id = 1;
    int prev = atomic_fetch_add(&next_id, 1);
    assert(prev == 1 && atomic_load(&next_id) == 2);
}

Linked from

Related notes