How does sig_atomic_t actually work?

前端 未结 5 2088
梦谈多话
梦谈多话 2020-11-30 02:19

How does the compiler or OS distinguish between sig_atomic_t type and a normal int type variable, and ensures that the operation will be atomic? Programs using both have sam

5条回答
  •  半阙折子戏
    2020-11-30 02:57

    This data type seems to be atomic.
    From here:

    24.4.7.2 Atomic Types To avoid uncertainty about interrupting access to a variable, you can use a particular data type for which access is always atomic: sig_atomic_t. Reading and writing this data type is guaranteed to happen in a single instruction, so there’s no way for a handler to run “in the middle” of an access.

    The type sig_atomic_t is always an integer data type, but which one it is, and how many bits it contains, may vary from machine to machine.

    Data Type: sig_atomic_t This is an integer data type. Objects of this type are always accessed atomically.

    In practice, you can assume that int is atomic. You can also assume that pointer types are atomic; that is very convenient. Both of these assumptions are true on all of the machines that the GNU C Library supports and on all POSIX systems we know of.

提交回复
热议问题