agentleFS
mohitmishra786mohitmishra786/low-level-dev-skills/skills/low-level-programming/memory-model/SKILL.md

memory-model

Skill303 starsChanged 10 days agoMITView on GitHub

C++ and Rust memory model skill for concurrent programming. Use when understanding memory ordering, writing lock-free data structures, using std::atomic or Rust atomics, diagnosing data races, or selecting the correct memory order for atomic operations. Also covers futexes (futex_waitv, futex2) and rseq. Activates on queries about memory ordering, acquire-release, seq_cst, relaxed atomics, happens-before, memory barriers, std::atomic, Rust atomic ordering, futex, futex_waitv, or rseq.

1 other project uses a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

4 rules. About 0 are common, 0 are distinctive, and 4 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈0

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 4

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Use skills/runtimes/sanitizers — TSan detects data races involving non-atomic accesses

    line 276
  2. Very specific:

    Use skills/rust/rust-sanitizers-miri for detecting Rust memory ordering violations with Miri

    line 277
  3. Very specific:

    Use skills/low-level-programming/assembly-x86 to understand generated fence instructions

    line 278
  4. Very specific:

    Use skills/debuggers/gdb for debugging concurrent programs with thread inspection

    line 279

≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Memory Model

Purpose

Guide agents through C++ and Rust memory models: memory orderings, the happens-before relation, atomic operations, fences, and practical patterns for lock-free data structures.

Triggers

  • "What is the C++ memory model?"
  • "What memory order should I use for my atomic operation?"
  • "What is the difference between acquire-release and seq_cst?"
  • "How do I use std::atomic in C++?"
  • "What is acquire/release in Rust atomics?"
  • "How do I implement a lock-free queue?"

Workflow

1. Memory ordering overview

Modern CPUs and compilers reorder operations for performance. The memory model specifies what reorderings are allowed and how synchronisation is achieved.

Ordering strength (weakest to strongest):
Relaxed < Release/Acquire < AcqRel < SeqCst

Stronger ordering = more synchronization = more correct, but slower
Weaker ordering  = fewer barriers = faster, but needs careful analysis

2. Memory orderings

OrderC++RustWhat it means
Relaxedmemory_order_relaxedOrdering::RelaxedNo ordering guarantee; just atomicity
Consumememory_order_consume(use Acquire)Data dependency ordering
Acquirememory_order_acquireOrdering::AcquireThis load sees all writes before the matching release
Releasememory_order_releaseOrdering::ReleaseAll writes before this store are visible to acquire
AcqRelmemory_order_acq_relOrdering::AcqRelBoth acquire and release on RMW ops
SeqCstmemory_order_seq_cstOrdering::SeqCstTotal order across all seq_cst operations

3. C++ std::atomic

#include <atomic>
#include <thread>

std::atomic<int> counter{0};
std::atomic<bool> ready{false};

// Producer thread
void producer() {
    data = 42;                              // (1) write data
    ready.store(true, std::memory_order_release);  // (2) signal
}

// Consumer thread
void consumer() {
    while (!ready.load(std::memory_order_acquire));  // (3) wait
    assert(data == 42);                              // (4) guaranteed to see (1)
}

Acquire-release guarantees: if thread A does a release store to X, and thread B does an acquire load that sees A's value, then all writes by A before the release are visible to B after the acquire.

4. Choosing the right ordering

Use case?
├── Counter (just needs atomicity, order irrelevant)    → Relaxed
├── Reference counting (decrement + final check)        → AcqRel (dec), Acquire (load 0 check)
├── Publish data from one thread to another             → Release (store), Acquire (load)
├── Mutual exclusion / mutex implementation             → AcqRel / SeqCst
├── Lock-free queue multiple producers/consumers        → SeqCst (safest to start)
└── Sequence number check (simple flag)                 → Release + Acquire

5. Common patterns

// Pattern 1: Spinlock
class Spinlock {
    std::atomic_flag flag = ATOMIC_FLAG_INIT;
public:
    void lock() {
        while (flag.test_and_set(std::memory_order_acquire))
            ; // spin
    }
    void unlock() {
        flag.clear(std::memory_order_release);
    }
};

// Pattern 2: Reference counting
class RefCounted {
    std::atomic<int> refcount{1};
public:
    void addref() {
        refcount.fetch_add(1, std::memory_order_relaxed);  // only need atomicity
    }
    void release() {
        if (refcount.fetch_sub(1, std::memory_order_acq_rel) == 1) {
            // AcqRel ensures we see all writes from other releasers
            delete this;
        }
    }
};

// Pattern 3: One-time initialisation
class LazyInit {
    std::atomic<void*> ptr{nullptr};
    std::mutex mtx;
public:
    void* get() {
        void* p = ptr.load(std::memory_order_acquire);
        if (p == nullptr) {
            std::lock_guard lock(mtx);
            p = ptr.load(std::memory_order_relaxed);
            if (p == nullptr) {
                p = create();
                ptr.store(p, std::memory_order_release);
            }
        }
        return p;
    }
};

6. Rust atomics

use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::Arc;

// Simple counter
let counter = Arc::new(AtomicUsize::new(0));

// Increment
counter.fetch_add(1, Ordering::Relaxed);

// Read
let val = counter.load(Ordering::Relaxed);

// Publish/subscribe pattern
static READY: AtomicBool = AtomicBool::new(false);

// Publisher thread
unsafe { DATA = 42; }  // Write data
READY.store(true, Ordering::Release);  // Signal

// Subscriber thread
while !READY.load(Ordering::Acquire) {}
let d = unsafe { DATA };  // Safe: guaranteed to see publisher's write

7. Fences

Fences provide ordering without an atomic operation on a specific variable:

// C++ fence — equivalent to a global memory barrier
std::atomic_thread_fence(std::memory_order_acquire);  // Acquire fence
std::atomic_thread_fence(std::memory_order_release);  // Release fence

// Typical use: multiple atomic writes then one fence
relaxed_atomic_a.store(1, std::memory_order_relaxed);
relaxed_atomic_b.store(2, std::memory_order_relaxed);
std::atomic_thread_fence(std::memory_order_release);  // barrier for all above
sentinel.store(true, std::memory_order_relaxed);

8. Common mistakes

MistakeFix
Using Relaxed for publish/subscribeUse Release on store, Acquire on load
Using SeqCst everywhereProfile first; use weakest correct ordering
Forgetting that non-atomic loads are not atomicAll shared mutable data needs atomic or mutex
Using volatile for thread safety in C++volatile is not a memory ordering tool; use atomic
Assuming sequential consistency without SeqCstEach platform has different default consistency

For memory ordering rules and happens-before reference, see references/cpp-memory-ordering.md.

9. Futexes and rseq

Futexes are the kernel half of every mutex and condition variable: user space spins or uses atomics, and calls into the kernel only to sleep or wake. Two newer interfaces matter for custom synchronization:

/* waitv.c — futex_waitv (Linux 5.16+): sleep on two futexes, wake on either */
#define _GNU_SOURCE
#include <linux/futex.h>
#include <pthread.h>
#include <stdatomic.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/syscall.h>
#include <time.h>
#include <unistd.h>

static _Atomic uint32_t f1, f2;

static long futex_waitv(struct futex_waitv *w, unsigned n, unsigned flags,
                        struct timespec *timeout, clockid_t clock)
{
    return syscall(SYS_futex_waitv, w, n, flags, timeout, clock);
}

static void *waker(void *arg)
{
    (void)arg;
    usleep(100 * 1000);
    atomic_store(&f2, 1);                                   /* change the value first */
    syscall(SYS_futex, &f2, FUTEX_WAKE_PRIVATE, 1, NULL, NULL, 0);
    return NULL;
}

int main(void)
{
    struct futex_waitv w[2] = {
        { .uaddr = (uintptr_t)&f1, .val = 0, .flags = FUTEX2_SIZE_U32 | FUTEX2_PRIVATE },
        { .uaddr = (uintptr_t)&f2, .val = 0, .flags = FUTEX2_SIZE_U32 | FUTEX2_PRIVATE },
    };
    pthread_t t;
    pthread_create(&t, NULL, waker, NULL);
    long idx = futex_waitv(w, 2, 0, NULL, CLOCK_MONOTONIC); /* returns index of the woken futex */
    printf("woken by futex %ld (f2=%u)\n", idx, atomic_load(&f2));   /* woken by futex 1 */
    pthread_join(t, NULL);
    return 0;
}
/* futex2 single-futex calls (Linux 6.7+): explicit size flags and a bitset mask */
syscall(SYS_futex_wait, &word, 0UL /* expected */, 0xffffffffUL /* mask */,
        FUTEX2_SIZE_U32 | FUTEX2_PRIVATE, NULL, CLOCK_MONOTONIC);
syscall(SYS_futex_wake, &word, 0xffffffffUL, 1 /* waiters */, FUTEX2_SIZE_U32 | FUTEX2_PRIVATE);

The mask must fit the futex size: ~0UL with FUTEX2_SIZE_U32 fails with EINVAL. Always re-check the value after waking; the kernel only promises the value was different or a wake happened.

Restartable sequences (rseq) give each thread a kernel-updated area with its current CPU. glibc 2.35+ registers it for every thread, which enables per-CPU data structures without atomics on the fast path (tcmalloc uses this):

/* rseq.c — read the current CPU from the rseq area glibc registers (glibc 2.35+) */
#define _GNU_SOURCE
#include <sched.h>
#include <stdio.h>
#include <sys/rseq.h>

int main(void)
{
    if (__rseq_size == 0) {                       /* registration disabled or unsupported */
        puts("rseq not registered");
        return 1;
    }
    struct rseq *rs = (struct rseq *)((char *)__builtin_thread_pointer() + __rseq_offset);
    printf("rseq cpu_id=%u sched_getcpu=%d size=%u\n", rs->cpu_id, sched_getcpu(), __rseq_size);
    return 0;
}

GLIBC_TUNABLES=glibc.pthread.rseq=0 disables glibc's registration (the program above then prints rseq not registered), which is needed when a library registers its own rseq area.

Related skills

  • Use skills/runtimes/sanitizers — TSan detects data races involving non-atomic accesses
  • Use skills/rust/rust-sanitizers-miri for detecting Rust memory ordering violations with Miri
  • Use skills/low-level-programming/assembly-x86 to understand generated fence instructions
  • Use skills/debuggers/gdb for debugging concurrent programs with thread inspection

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.