Set thread-local sig_atomic_t in signal handler?
Ian Lance Taylor
iant@google.com
Thu Sep 26 16:34:00 GMT 2013
On Thu, Sep 26, 2013 at 8:34 AM, Ian Pilcher <arequipeno@gmail.com> wrote:
> Short version:
>
> Can I use a thread-local sig_atomic_t in a signal handler? I.e.:
>
> static __thread volatile sig_atomic_t exit_flag = 0;
>
> static void handler(int signum __attribute__((unused)))
> {
> exit_flag = 1;
> }
>
> (Signals will be sent to specific threads with pthread_kill.)
Yes. This is one of the few valid uses of volatile in a
multi-threaded program.
That said, a warning: if you put this code in a shared library and
open it with dlopen it may fail in a rather peculiar way. See
https://sourceware.org/ml/libc-alpha/2012-06/msg00335.html
I think this behaviour is a bug, but I'm not sure everyone agrees with
me, and in any case it has not been fixed.
Ian
More information about the Gcc-help
mailing list