Set thread-local sig_atomic_t in signal handler?

Ian Pilcher arequipeno@gmail.com
Thu Sep 26 17:14:00 GMT 2013


On 09/26/2013 11:34 AM, Ian Lance Taylor wrote:
> 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.

Cool.  I wasn't sure if mixing __thread and volatile would have the
desired result or whether thread-local variables were even accessible
within a signal handler.

> 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.

Interesting thread.  Reading it actually makes me feel more confident
that my particular usage falls into the "expected to work" category,
rather than "happens to work for now".

Thanks!

-- 
========================================================================
Ian Pilcher                                         arequipeno@gmail.com
Sometimes there's nothing left to do but crash and burn...or die trying.
========================================================================



More information about the Gcc-help mailing list