This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Set thread-local sig_atomic_t in signal handler?
- From: Ian Lance Taylor <iant at google dot com>
- To: Ian Pilcher <arequipeno at gmail dot com>
- Cc: "gcc-help at gcc dot gnu dot org" <gcc-help at gcc dot gnu dot org>
- Date: Thu, 26 Sep 2013 09:34:18 -0700
- Subject: Re: Set thread-local sig_atomic_t in signal handler?
- Authentication-results: sourceware.org; auth=none
- References: <52445407 dot 4020507 at gmail dot com>
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