mercredi 30 mai 2018

C++11 synchronisation in Linux signal handler

I have a multithreaded Linux app that is required to terminate smoothly in response to SIGTERM.

The way it's currently implemented uses a std::promise<void> and associated future: the main thread first installs a simple SIGTERM handler that sets the promise and returns, then it launches all the subsidiary threads, and then it waits on the future. When SIGTERM is received, the handler sets the promise. This unblocks the main thread, which controls orderly shutdown of the subsidiary threads and then exits.

This all works fine - but as far as I can tell there's actually no guarantee that it's safe to use std::promise::set_value() (or, for that matter, any other C++11 synchronisation mechanism) within the signal handler.

Is this actually risky in practice? Should I replace this with e.g. sem_post or some other mechanism explicitly described as safe?

Aucun commentaire:

Enregistrer un commentaire