Skip to content
Back to skills

Writing Lock Free Code

ASecurity

Use when writing or reviewing shared-memory concurrent code with atomics, lock-free data structures, or custom synchronization. Triggers on memory ordering (relaxed/acquire/release/seq_cst), CAS/compare-exchange, the ABA problem, SPSC/MPSC/MPMC queues, lock-free stacks, hazard pointers, epoch or RCU reclamation, false sharing, spinlocks and backoff, work-stealing, even when the user doesn't say 'lock-free'.

  • 6 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added November 30, 2026
ai-agentsgonodetesting

Security analysis

A100/100

Pro scans all 19 files and shows the line behind each finding

Scanned November 30, 2026

npx -y skills add xonovex/platform --skill writing-lock-free-code --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Writing Lock Free Code?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Writing Lock Free Code
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/xonovex-writing-lock-free-code/badge)](https://www.skillsdirectory.com/skills/xonovex-writing-lock-free-code)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: writing-lock-free-code
description: "Use when writing or reviewing shared-memory concurrent code with atomics, lock-free data structures, or custom synchronization. Triggers on memory ordering (relaxed/acquire/release/seq_cst), CAS/compare-exchange, the ABA problem, SPSC/MPSC/MPMC queues, lock-free stacks, hazard pointers, epoch or RCU reclamation, false sharing, spinlocks and backoff, work-stealing, even when the user doesn't say 'lock-free'."
---

# Lock-Free / Wait-Free Concurrency Guidelines

Choose concurrency primitives by their progress guarantees, memory ordering, and reclamation requirements; verify them under contention.

## Requirements

- C11 atomics (`<stdatomic.h>`); applies to systems code with atomic, threading, and job-scheduling primitives.

## Essentials

- **No data races** - Every shared mutable location touched by 2+ threads is atomic or lock-protected, see [references/memory-model.md](references/memory-model.md)
- **CAS loops retry** - `compare_exchange_weak` in a loop; reload the expected value on failure, see [references/atomics-and-cas.md](references/atomics-and-cas.md)
- **Lock-free needs reclamation** - Removing a node from a shared structure is not freeing it, see [references/safe-memory-reclamation.md](references/safe-memory-reclamation.md)
- **Measure, do not assume** - Locks are often faster; profile before going lock-free, see [references/progress-guarantees.md](references/progress-guarantees.md)

## Memory model

- **Progress guarantees** - wait-free ⊃ lock-free ⊃ obstruction-free ⊃ blocking, see [references/progress-guarantees.md](references/progress-guarantees.md)
- **Happens-before** - Acquire/release builds the ordering that makes writes visible, see [references/memory-model.md](references/memory-model.md)
- **Orderings** - relaxed/acquire/release/acq_rel/seq_cst + fences, see [references/memory-ordering.md](references/memory-ordering.md)

## Building blocks

- **Atomics & CAS** - load/store/exchange/fetch\_\*/compare_exchange, weak vs strong, see [references/atomics-and-cas.md](references/atomics-and-cas.md)
- **ABA problem** - CAS can't see a value changed away and back; tag pointers or reclaim safely, see [references/aba-problem.md](references/aba-problem.md)
- **False sharing** - Pad/align hot per-thread and atomic fields to their own cache line, see [references/false-sharing.md](references/false-sharing.md)

## Data structures

- **SPSC ring** - One producer, one consumer, no CAS, see [references/spsc-ring-buffer.md](references/spsc-ring-buffer.md)
- **MPSC queue** - Intrusive: producers XCHG tail, consumer walks next, see [references/mpsc-queue.md](references/mpsc-queue.md)
- **MPMC bounded** - Per-cell sequence numbers, ABA-free, see [references/mpmc-bounded-queue.md](references/mpmc-bounded-queue.md)
- **Lock-free stack** - CAS on head; needs reclamation for pop, see [references/lock-free-stack.md](references/lock-free-stack.md)

## Reclamation & scaling

- **Safe reclamation** - refcount / hazard pointers / epoch (QSBR) / RCU, see [references/safe-memory-reclamation.md](references/safe-memory-reclamation.md)
- **Locks & backoff** - TTAS spinlock, exponential backoff, ticket/MCS/CLH, seqlock, see [references/locks-and-backoff.md](references/locks-and-backoff.md)
- **Scale by not sharing** - Shard, partition, combine, work-steal, NUMA-place, see [references/scalable-architecture.md](references/scalable-architecture.md)

## Gotchas

- A successful CAS proves only that the bit pattern matched, not that nothing happened in between (ABA); pointers reused after free defeat it.
- `volatile` is not atomic and gives no ordering. It never makes concurrent C correct; use `_Atomic`/`atomic_*`.
- `compare_exchange_weak` may fail spuriously even when the value matches, only ever use it inside a retry loop.
- Removing a node from a lock-free list does not mean no other thread is still dereferencing it: freeing too early is a use-after-free.
- Two unrelated atomics on the same cache line still contend (false sharing): alignment, not correctness, but it can erase all your scaling.

## Progressive Disclosure

- Read [references/progress-guarantees.md](references/progress-guarantees.md) - Load when deciding between locks, lock-free, or wait-free, or explaining the trade-offs
- Read [references/memory-model.md](references/memory-model.md) - Load when reasoning about data races, visibility, or happens-before
- Read [references/memory-ordering.md](references/memory-ordering.md) - Load when choosing or reviewing a `memory_order_*` argument or a fence
- Read [references/atomics-and-cas.md](references/atomics-and-cas.md) - Load when writing atomic loads/stores/RMW or a compare-exchange loop
- Read [references/aba-problem.md](references/aba-problem.md) - Load when a CAS-based structure reuses memory or pointers
- Read [references/false-sharing.md](references/false-sharing.md) - Load when per-thread counters or atomics scale badly under contention
- Read [references/spsc-ring-buffer.md](references/spsc-ring-buffer.md) - Load when building a single-producer/single-consumer queue
- Read [references/mpsc-queue.md](references/mpsc-queue.md) - Load when many threads feed one consumer (logging, job results)
- Read [references/mpmc-bounded-queue.md](references/mpmc-bounded-queue.md) - Load when building a general bounded multi-producer/multi-consumer queue
- Read [references/lock-free-stack.md](references/lock-free-stack.md) - Load when implementing a lock-free stack or freelist
- Read [references/safe-memory-reclamation.md](references/safe-memory-reclamation.md) - Load when freeing nodes that other threads might still read
- Read [references/locks-and-backoff.md](references/locks-and-backoff.md) - Load when a lock is the right tool and you need a scalable one
- Read [references/scalable-architecture.md](references/scalable-architecture.md) - Load when synchronization itself is the bottleneck
- Read [references/testing-and-verification.md](references/testing-and-verification.md) - Load when validating concurrent code before merge

Files in this skill

  • SKILL.md5.9 KB
  • eval-output.json4 KB
  • eval-trigger.json5.5 KB
  • moon.yml86 B
  • references/aba-problem.md2.7 KB
  • references/atomics-and-cas.md3.3 KB
  • references/false-sharing.md1.9 KB
  • references/lock-free-stack.md2.9 KB
  • references/locks-and-backoff.md4.1 KB
  • references/memory-model.md2.8 KB
  • references/memory-ordering.md2 KB
  • references/mpmc-bounded-queue.md5.3 KB
  • references/mpsc-queue.md3.6 KB
  • references/progress-guarantees.md2.6 KB
  • references/safe-memory-reclamation.md4.5 KB
  • references/scalable-architecture.md4.1 KB
  • references/spsc-ring-buffer.md2.6 KB
  • references/testing-and-verification.md3.4 KB
  • sources.json7 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in ai-agents →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.