Linux signal handling - notifying a condition variable

Dmitry Olshansky dmitry.olsh at gmail.com
Thu Mar 15 19:23:26 UTC 2018


On Thursday, 15 March 2018 at 17:30:50 UTC, Jim King wrote:
> On Thursday, 15 March 2018 at 17:12:24 UTC, Adam D. Ruppe wrote:
>> On Thursday, 15 March 2018 at 16:51:59 UTC, Jim King wrote:
>>> In going through the signal documentation it looks like the 
>>> signal handler must be a "nothrow @nogc" variety.
>>
>> Looks like notify actually can throw an exception... the way I 
>> usually do signal handlers is just set a global variable:
>>
>> __global bool interrupted = false;
>> void sigint_handler() { interrupted = true; }
>>
>> then in the main loop check that periodically... idk if that'd 
>> work well withthe condition variable though, I have never 
>> actually used that...
>
> Thanks for that suggestion; I was in the middle of implementing 
> that.  The problem with that is that it requires a busy loop to 
> detect it.  It is far better to block on condition variables 
> than to have a busy loop.

Another option if you are on linux is to use eventfd. Then you 
can trigger it with simple write on eventfd descriptor.

As far as waiting goes it’s either read on descriptor or 
poll/select.
f it seems a bit more involved.
> Condition variables (and mutexes) are supposed to be usable 
> from a signal handler.

However I’d be super careful about mutexes with signal handlers. 
Really self-pipe trick or eventfd seems way more signal safe.





More information about the Digitalmars-d mailing list