boost signal 에 대해서 질문드립니다.

trymp의 이미지


아래와 같이 boost::asio 로 signal 처리를 할 때 handler() 함수가 signal context 영역에 들어가는 건가요?

아니면 io_service 내부에서 signal 마킹만 하고 적당한 시점에 호출하는 것인지 궁금합니다.

아래와 같은 코드에서 signal safe 해야하는지 궁금해서 질문드립니다.

고수님들의 조언 부탁드립니다.

#include <boost/asio.hpp>
#include <boost/asio/signal_set.hpp>
#include <boost/bind.hpp>
#include <boost/atomic.hpp>
#include <iostream>
 
void handler(boost::asio::signal_set& this_, boost::system::error_code error, int signal_number) {
    if (!error) {
        static boost::atomic_bool first(true);
        if(first) {
            // do something like writing data to a file
            std::cout << " A signal(SIGINT) occurred." << std::endl;
            first = false;
 
            this_.async_wait(boost::bind(handler, boost::ref(this_), _1, _2));
        }
        else {
            std::cout << " A second signal(SIGINT) occurred, exiting...." << std::endl;
            exit(1);
        }
    }
}
 
int main() {
    // Construct a signal set registered for process termination.
    boost::asio::io_service io;
    boost::asio::signal_set signals(io, SIGINT);
 
    // Start an asynchronous wait for one of the signals to occur.
    signals.async_wait(boost::bind(handler, boost::ref(signals), _1, _2));
    io.run();
    return 2;
}