[C++] 适用程序退出的Signal Handler

To the point


#include 
#include 
#include 
#include 

class SignalHandler {
public:
    SignalHandler() = delete;

    static void hookSIGINT() {
        ::signal(SIGINT, handleUserInterrupt);
    }

    static void waitForUserInterrupt() {
        std::unique_lock lock(mutex_);
        cond_.wait(lock);
        LOG(INFO) << "User has signaled to interrupt program..." << std::endl;
    }

private:
    static void handleUserInterrupt(int signo) {
        if (signo == SIGINT) {
            LOG(INFO) << "SIGINT trapped ..." << std::endl;
            cond_.notify_one();
        }
    }

private:
    static std::mutex mutex_;
    static std::condition_variable cond_;
};

你可能感兴趣的:(c++)