How can I set a variable of type long
(on 64 bit machine = 8 bytes) inside a signal handler? I've read that you can only use variables of type sig_atomic_t
, which is actually implemented as volatile int
inside a signal handler and it is unsafe to modify data types bigger than an int
.
相关问题
- Sorting 3 numbers without branching [closed]
- Multiple sockets for clients to connect to
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
You can use a
long
inside a signal handler, you can use anything, in fact. The only thing you should take care of is proper synchronization in order to avoid race conditions.sig_atomic_t
should be used for variables shared between the signal handler and the rest of the code. Any variable "private" to the signal handler can be of any type, any size.Sample code :
If you want to use a shared variable other than
sig_atomic_t
use atomics (atomic_long_read
,atomic_long_set
).