gpt4 book ai didi

c++ - SIGINT 正常关闭

转载 作者:行者123 更新时间:2023-11-30 05:11:17 25 4
gpt4 key购买 nike

我想处理来自内核的 SIGINT 信号,以便调用一个函数来优雅地关闭我的进程。
这是一个工作示例,一个线程正在等待信号,然后调用函数 handle_stop:

#include <iostream>
#include <boost/asio.hpp>
#include <boost/thread.hpp>

void handle_stop(const boost::system::error_code& error, int signal_number) {
std::cout<<"Executing Safe Shutdown"<<std::cout;
exit(0);
}

int main() {
std::cout<<"Init"<<std::cout;
boost::asio::io_service signalService;
boost::asio::signal_set signals(signalService, SIGINT, SIGTERM, SIGQUIT);
signals.async_wait(handle_stop);
boost::thread signalThread(boost::bind(&boost::asio::io_service::run, &signalService));

std::cout<<"Starting programm"<<std::cout;
while (true) {
std::cout<<"Waiting ctl-c"<<std::cout;
sleep(1);
}
}

我的目标是将线程和关闭进程调用的函数打包到一个类中。
这是一个无效的尝试,进程立即关闭而不等待信号
怎么了?

#include <atomic>
#include <iostream>
#include <boost/asio.hpp>
#include <boost/thread.hpp>

class Shutdown {
public:
Shutdown():is_signal_received_ (false) {
//signals_::remove();
std::cout<<"constructor"<<std::endl;
}
~Shutdown() {
}

void init() {
std::cout<<"Init "<<std::endl;
boost::asio::signal_set signals(signalService_, SIGINT, SIGTERM, SIGQUIT);
signals.async_wait(boost::bind(boost::mem_fn(&Shutdown::handleStop), this, _1, _2));
boost::thread signalThread(boost::bind(&boost::asio::io_service::run, &signalService_));
std::cout<<"Init Completed"<<std::endl;
}

bool isSignalReceived() const {
return is_signal_received_;
}

private:
std::atomic<bool> is_signal_received_;
boost::asio::io_service signalService_;

void handleStop(const boost::system::error_code& error, int signal_number) {
is_signal_received_ = true;
myHandleStop(error, signal_number);
}

virtual void myHandleStop(const boost::system::error_code& error, int signal_number) {
}
};

class MyShutdown: public Shutdown {
private:
void myHandleStop(const boost::system::error_code& error, int signal_number) {
std::cout<<"Executing Safe Shutdown"<<std::cout;
exit(0);
}
};

int main() {
MyShutdown safeShutdown;
safeShutdown.init();
while (true) {
std::cout<<"Waiting ctl-c"<<std::cout;
sleep(1);
}
}

编译命令如下:

g++ -o main main.cpp -lpthread -l boost_thread -l boost_system --std=c++11

最佳答案

您的问题是您的 signal_set 超出范围并在 Shutdown::init 结束时被销毁。发生这种情况时,async_wait 将被取消。 signalThread 也同时超出范围,没有被 detached 或 joined。两者都需要成为类(class)成员,这样他们才能在信号被处理之前一直活着:

#include <atomic>
#include <iostream>
#include <boost/asio.hpp>
#include <boost/thread.hpp>

class Shutdown {
public:
Shutdown()
: is_signal_received_ (false),
signalService_(),
signals_(signalService_, SIGINT, SIGTERM, SIGQUIT)
{
std::cout<<"constructor"<<std::endl;
}
~Shutdown()
{
signals_.cancel();
signalService_.stop();
signalThread_.join();
}

void init() {
std::cout<<"Init "<<std::endl;
signals_.async_wait(boost::bind(&Shutdown::handleStop, this, _1, _2));
signalThread_ = boost::thread(boost::bind(&boost::asio::io_service::run, &signalService_));
std::cout<<"Init Completed"<<std::endl;
}

bool isSignalReceived() const {
return is_signal_received_;
}

private:
std::atomic<bool> is_signal_received_;
boost::asio::io_service signalService_;
boost::thread signalThread_;
boost::asio::signal_set signals_;

void handleStop(const boost::system::error_code& error, int signal_number) {
is_signal_received_ = true;
myHandleStop(error, signal_number);
}

virtual void myHandleStop(const boost::system::error_code& error, int signal_number) {
}
};

class MyShutdown: public Shutdown {
private:
void myHandleStop(const boost::system::error_code& error, int signal_number) {
std::cout<<"Executing Safe Shutdown"<<std::endl;
exit(0);
}
};

int main() {
MyShutdown safeShutdown;
safeShutdown.init();
while (true) {
std::cout<<"Waiting ctl-c"<<std::endl;
sleep(10);
}
}

我还添加了调用以关闭 io_servicesignal_set 并等待 thread 中终止~关机

关于c++ - SIGINT 正常关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45137048/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com