QBATOMIC.H(3)

✨ QBATOMIC.H ✨

okay bestie, so this is qbatomic.h from the libqb programmer's manual, and it's literally the blueprint for Basic atomic integer and pointer operations πŸ’… like the name says. this is the file you include when you want your threads to stop fighting over integers, periodt.


🌸 SYNOPSIS 🌸

#include <qb/qbatomic.h>

that's it. one include. iconic behavior tbh πŸŽ€


πŸ’– DESCRIPTION πŸ’–

so here's the tea: the functions in here let you access integers and pointers atomically. on most platforms they're implemented as inline assembler functions, and everywhere else they fall back to slower versions. the whole point? sometimes using them saves you from dragging a performance-expensive pthread_mutex in just to protect one lil integer or pointer. slay. πŸ¦‹

the most important usage is reference counting, no cap. using qb_atomic_int_inc() and qb_atomic_int_dec_and_test() makes reference counting a very fast operation. it's giving speed, it's giving efficiency. πŸ”₯

okay now the rules, bestie, listen up πŸ‘‘

you must not directly read integers or pointers that are concurrently accessed by multiple threads. like, don't even. use the atomic accessor functions instead. that means: always use qb_atomic_int_get() and qb_atomic_pointer_get() for your read-outs. they provide the necessary synchronization mechanisms β€” memory barriers and all that β€” to access memory locations concurrently. ✨

if you're using these functions for anything apart from simple reference counting, you should really be aware of the implications of doing that. there are literally thousands of ways to shoot yourself in the foot. so if in doubt, use a pthread_mutex. and if you don't know what memory barriers are? do not use anything but qb_atomic_int_inc() and qb_atomic_int_dec_and_test(). that's the vibe check. πŸ’€

also, it is not safe to set an integer or pointer just by assigning to it when it's concurrently accessed by other threads with the following functions. use qb_atomic_int_compare_and_exchange() or qb_atomic_pointer_compare_and_exchange() respectively instead. 😭

πŸ’… SEE ALSO πŸ’…

go read them, bestie, they're the whole friend group 🫢


Copyright (C) 2003 Sebastian Wilhelmi


and that's the whole page, gorgeous 🌈 this came out of libqb, dated 2025-09-16, and you found it filed under QBATOMIC.H(3) β€” go forth and atomically slay πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from