Table of Contents


qthread_cas, qthread_cas_ptr - atomically compare-and-swap a value


#include <qthread.h>

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

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


This function atomically compares the contents of the memory at addr with the value oldval and if they are the same, writes newval into that memory. If they are not the same, no change is made. In either case, the contents of addr are returned. In short, this is an atomic version of:

if (*addr == oldval) {

*addr = newval;
return *addr;

This function uses architecture-specific assembly to achieve this, or compiler built-in functions, if they exist.

Return Value

These functions will return the value of *addr before any assignment is (potentially) performed.

See Also


Table of Contents