Format
#include <builtin.h>
unsigned long long __cmpxchg8(unsigned long long *op1, unsigned long long op2,
unsigned long long value)
Language Level: None
__cmpxchg8 compares an 8 byte memory operand, *op1, with
value. If equal, op2 is stored into *op1.
The compare and exchange operation is only atomic on
uni-processor machines. This builtin function will not work on a
386 or 486 processor.
Return Value
The original value of the memory operand is returned.
Example
This example compares sem with sem_free,
determines they are equal, and exchanges the value of sem
and thread. It does all this atomically on uni-processor
machines.
#include <stdio.h> #include <builtin.h>
const unsigned long long sem_free = 0; unsigned long long sem = sem_free; unsigned long long thread = 1;
int main(void)
{
if (__cmpxchg8(&sem, thread, sem_free) == sem_free)
{
printf("The exchange occurred.\n");
}
printf("sem = %llu, thread = %llu, sem_free = %llu\n", sem, thread, sem_free);
/*****************************************
The output should be:
The exchange occurred.
sem = 1, thread = 1, sem_free = 0
*****************************************/
return 0; }
![]()
__cmpxchg4 -- Compare and
Exchange 4 Byte Values
__smp_cmpxchg4 -- Compare and Exchange 4
Byte Values Atomically
__smp_cmpxchg8 -- Compare and Exchange 8
Byte Values Atomically