Skip to main content

Checkpoint: SPSC Ring Buffer Core

Challenge on lesson: Checkpoint: SPSC Ring Buffer Core

CHALLENGE
Difficulty: advanced+25 XP

ISO C11. Given a pre-filled ring state struct ring { atomic_size_t head; atomic_size_t tail; int buf[8]; }; r (head/tail both start at 0; empty means head==tail; full means (head+1)%8==tail โ€” one slot sacrificed). Implement int ring_push(struct ring *r, int v) (store v at buf[head % 8], then advance head with release ordering; return 0, or -1 when full) and int ring_pop(struct ring *r, int *out) (load tail with acquire, check emptiness, read buf[tail % 8] into *out, advance tail relaxed; return 0, or -1 when empty). NULL r (or NULL out for pop) returns -1.

Back to lesson: Checkpoint: SPSC Ring Buffer Core