Are sig_atomic_t and std::atomic<> interchangab

2020-01-29 08:23发布

问题:

As per the title. Can I use std::atomic<> in a signal handler or does sig_atomic_t provide other compiler features?

回答1:

n3376 1.9/6

When the processing of the abstract machine is interrupted by receipt of a signal, the values of objects which are neither

— of type volatile std::sig_atomic_t nor

— lock-free atomic objects (29.4)

are unspecified during the execution of the signal handler, and the value of any object not in either of these two categories that is modified by the handler becomes undefined.

Lock-free 29.4/1,2

The ATOMIC_..._LOCK_FREE macros indicate the lock-free property of the corresponding atomic types, with the signed and unsigned variants grouped together. The properties also apply to the corresponding (partial) specializations of the atomic template. A value of 0 indicates that the types are never lock-free. A value of 1 indicates that the types are sometimes lock-free. A value of 2 indicates that the types are always lock-free.

The function atomic_is_lock_free (29.6) indicates whether the object is lock-free. In any given program execution, the result of the lock-free query shall be consistent for all pointers of the same type.