我有一个应用程序,它有一个io_service和一个用于监视的服务器,所以我在不同的线程上启动了两件事:
int main(int argc, char** argv)
{
//...
sigset_t new_mask;
sigfillset(&new_mask);
sigset_t old_mask;
pthread_sigmask(SIG_BLOCK, &new_mask, &old_mask);
boost::asio::io_service io_service;
BasicIOObject< BasicIOService< > > myio(io_service);
// Register signal handlers so that the daemon may be shut down. You may
// also want to register for other signals, such as SIGHUP to trigger a
// re-read of a configuration file.
boost::asio::signal_set signals(io_service, SIGINT, SIGTERM);
signals.async_wait(boost::bind(&boost::asio::io_service::stop, &io_service));
blur.async_wait(boost::bind(&BasicIOObject<BasicIOService<>>::wait_handler, boost::ref(myio), boost_ph::error));
http::server::Server s("localhost", "5000");
// The io_service can now be used normally.
boost::thread t(boost::bind(&http::server::Server::run, &s));
boost::thread t2(boost::bind(&boost::asio::io_service::run, &io_service));
t2.interrupt();
t.interrupt();
// Restore previous signals.
pthread_sigmask(SIG_SETMASK, &old_mask, 0);
// Wait for signal indicating time to shut down.
sigset_t wait_mask;
sigemptyset(&wait_mask);
sigaddset(&wait_mask, SIGINT);
sigaddset(&wait_mask, SIGQUIT);
sigaddset(&wait_mask, SIGTERM);
pthread_sigmask(SIG_BLOCK, &wait_mask, 0);
int sig = 0;
sigwait(&wait_mask, &sig);
// Stop the server.
s.stop();
// Stop service
io_service.stop();
t.join();
t2.join();
}
服务器基于boost example
我在io_service的代码的某些部分添加了:
boost::this_thread::interruption_point();
if (boost::this_thread::interruption_enabled())
std::cout << "Interruption enabled" << std::endl;
else
std::cout << "Interruption disabled" << std::endl;
if (boost::this_thread::interruption_requested())
std::cout << "Requested interruption true" << std::endl;
else
std::cout << "Requested interruption false" << std::endl;
没有发生中断。控制台写的更多
Interruption enabled
Requested interruption false
我做错了什么?为什么它不会中断线程?