### __cmpxchg4 -- Compare and Exchange 4 Byte Values

Format

>     #include <builtin.h>

>     unsigned int __cmpxchg4(unsigned int *op1, unsigned int op2, unsigned int value)

Language Level: None
__cmpxchg4 compares a 4 byte memory operand, *op1, with value. If equal, op2 is stored into *op1. This operation is only atomic on uni-processor machines. This function will not work on a 386 processor.

Return Value
The original value of the memory operand (*op1) 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 int sem_free = 0;
    unsigned int sem = 0;
    unsigned int thread = 1;

    int main(void)
    {
       if (__cmpxchg4(&sem, thread, sem_free) == sem_free)
       {
          printf("The exchange occurred.\n");
       }

       printf("sem = %u, thread = %u, sem_free = %u\n", sem, thread, sem_free);

       /*****************************************
          The output should be:

          The exchange occurred.
          sem = 1, thread = 1, sem_free = 0
       *****************************************/

       return 0;
    }

__cmpxchg8 -- Compare and Exchange 8 Byte Values
__smp_cmpxchg4 -- Compare and Exchange 4 Byte Values Atomically
__smp_cmpxchg8 -- Compare and Exchange 8 Byte Values Atomically