CKTIS / C
atomic_signal_fence — C23 fence function
`atomic_signal_fence(order)` establishes ordering constraints only between thread code and a signal handler executed in that same thread. It has the same order classes as `atomic_thread_fence`, returns no value, and is not a generic function.
Version: C23
Syntax
void atomic_signal_fence(memory_order order);atomic_signal_fence(order) establishes ordering constraints only between thread code and a signal handler executed in that same thread. It has the same order classes as atomic_thread_fence, returns no value, and is not a generic function.
Use it when compiler movement across a boundary observable by a signal handler must be prevented. The example stores payload = 42, executes a release fence, invokes the handler through raise, and the handler copies the value to observed after an acquire fence, so the program prints 42.
A signal fence inhibits compiler optimizations and access reordering like a thread fence, but emits no hardware fence instruction and does not synchronize different threads. memory_order_relaxed has no effect; acquire/consume, release, acq_rel, and seq_cst have their corresponding roles. A fence alone does not make ordinary objects safe for an asynchronous handler—the example deliberately uses volatile sig_atomic_t and synchronous raise.
Examples
#include <stdatomic.h>
#include <signal.h>
#include <stdio.h>
static volatile sig_atomic_t payload;
static volatile sig_atomic_t observed;
static void handler(int signal_number) {
(void)signal_number;
atomic_signal_fence(memory_order_acquire);
observed = payload;
}
int main(void) {
if (signal(SIGTERM, handler) == SIG_ERR) return 1;
payload = 42;
atomic_signal_fence(memory_order_release);
if (raise(SIGTERM) != 0) return 1;
printf("%d\n", (int)observed);
return 0;
}
Result
42
Use it when compiler movement across a boundary observable by a signal handler must be prevented. The example stores `payload = 42`, executes a release fence, invokes the handler through `raise`, and the handler copies the value to `observed` after an acquire fence, so the program prints `42`.