Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

atomic_flag_clear — C23 atomic function

`atomic_flag_clear(object)` from `<stdatomic.h>` atomically places the pointed-to `atomic_flag` in the clear state and returns no value. The basic form performs the operation with `memory_order_seq_cst`.

Version: C23

Syntax

void atomic_flag_clear(volatile atomic_flag *object);

atomic_flag_clear(object) from <stdatomic.h> atomically places the pointed-to atomic_flag in the clear state and returns no value. The basic form performs the operation with memory_order_seq_cst.

Use it to release an ownership flag after the protected section is complete. The example really sets the flag, clears it, and then atomic_flag_test_and_set returns 0, proving that the preceding state was clear.

The flag must be properly initialized, for example with ATOMIC_FLAG_INIT, and the pointer must be valid. Clearing a lock from a thread that does not own it breaks the protocol despite atomicity. atomic_flag_clear_explicit must not use memory_order_acquire or memory_order_acq_rel; release ordering is typical for spinlock unlock.

Examples

#include <stdatomic.h>
#include <stdio.h>

int main(void) {
    atomic_flag flag = ATOMIC_FLAG_INIT;
    (void)atomic_flag_test_and_set(&flag);
    atomic_flag_clear(&flag);
    printf("%d\n", atomic_flag_test_and_set(&flag));
    return 0;
}

Result

0

Use it to release an ownership flag after the protected section is complete. The example really sets the flag, clears it, and then `atomic_flag_test_and_set` returns `0`, proving that the preceding state was clear.

Sources