If you use boost::asio you can handle signals with async_wait() method of boost::asio::signal_set. Handler set with async_wait() can do what you want, it is not called in signal handler context.
Calling functions from std::signal handler; SIGWINCH
質問
I need to capture the event of a terminal resize, to do this I am capturing (although other methods are welcome) SIGWINCH with the following:
std::signal(SIGWINCH, &handle_sigwinch);
I was then hoping to have handle_sinwinch
do something like:
display_type *dt; //inited elsewhere
void handle_sinwinch(int sig) {
dt->async_redraw();
}
Where async_redraw
post the work to do onto an internal boost::asio::io_service
and then returns. Thus there should be no race conditions; however, the doc for std::signal
seems to imply that I can safely ONLY manipulate volatile std::sig_atomic_t
s.
IE I can set a flag, then pick it up from the main thread as a part of an event loop.
The problem is that this is asynchronous so there is no while(true)
loop or anything like that. The only way to loop over such a flag would be to create a deadline timer and check it periodically. Which seems like a real hack.
So my question is, is my understanding of signals correct? And any insight how to do this would be appreciated.
解決