qthread_cas(3)

πŸ“¦ version 1.22^20251007gitcedb1fc, in fedora 43, fedora 44

πŸ’… qthread_cas β€” atomically compare-and-swap a value

okay bestie, welcome to libqthread ✨ this is the page for qthread_cas and qthread_cas_ptr, and no cap, they're kind of iconic. let's get into it 🫢


✨ NAME ✨

qthread_cas, qthread_cas_ptr β€” atomically compare-and-swap a value πŸ’–


🌸 SYNOPSIS 🌸

#include <qthread.h>

aligned_t
qthread_cas (volatile aligned_t *addr, aligned_t oldval,
             aligned_t newval);

void *
qthread_cas_ptr (void * volatile * addr, void * oldval,
                 void * newval);

πŸ¦‹ DESCRIPTION πŸ¦‹

so here's the vibe, bestie: this function atomically compares what's chillin' in the memory at addr with the value oldval, and if they're the same, it writes newval into that memory. if they're not the same? no change, nada, we leave it alone πŸ™ˆ

and either way β€” changed or not β€” the contents of addr get returned to you. so it's basically an atomic glow-up of this whole situation:

retval = *addr;
if (*addr == oldval) {
       *addr = newval;
}
return retval;

to pull this off, it uses architecture-specific assembly, or compiler built-in functions if those exist. lowkey genius tbh 🧚


⭐ RETURN VALUE ⭐

these functions return the value of *addr before any assignment is (potentially) performed. so you always get the receipts of what was there first, periodt πŸ’…


🌈 SEE ALSO 🌈

qthread_incr(3)


and that's the whole page, bestie!! this was qthread_cas(3) from libqthread, dated NOVEMBER 2009 β€” yes she's vintage, she's still slaying, we love her πŸ‘‘πŸ’œ

πŸ“¦ where this page comes from