Format
#include <builtin.h>unsigned long long __smp_cmpxchg8(unsigned long long *op1, unsigned long long op2, unsigned long long value)
Language Level: None
__smp_cmpxchg8 compares an 8 byte memory operand, *op1,
with value. If equal, op2 is stored into *op1.
The compare and exchange operation is atomic even on
multi-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.
#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 (__smp_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
__cmpxchg8 -- Compare and Exchange 8 Byte
Values
__smp_cmpxchg4 -- Compare and Exchange 4
Byte Values Atomically