C++/pthread/join 错误消息“what(): Invalid argument”的含义

scr*_*foo 2 c++ pthreads

它是一个更大的应用程序的一部分,一个简单的计时器线程,在尝试完成它之前可以正常工作:

boost::asio::io_service io_service;
shared_ptr<thread> loop;
// it is not initialised here, just shows the idea
boost::asio::deadline_timer timer(io_service, interval);

void Timer::spin() {
    loop = make_shared<thread>(&Timer::run, this);
}
void Timer::unspin() {
    io_service.stop();
    loop.get()->join();
}
void run() {
   timer.async_wait(bind(&Timer::callbackWrapper, this, _1));
   // restart
   io_service.run();    
}
Run Code Online (Sandbox Code Playgroud)

spin理论上创建的线程在 中终止并加入unspin()。如果应用程序以join()注释掉结束,则会出现错误消息

terminate called without an active exception
Run Code Online (Sandbox Code Playgroud)

如果我很好地“解密”它,则意味着“当其他一些线程仍在运行时主线程已完成”(我不知道 C++ 术语。)

如果依次unspin()调用,可能会显示以下不稳定的错误消息:

terminate called after throwing an instance of 'std::system_error'
  what():  Invalid argument
Run Code Online (Sandbox Code Playgroud)

这很可能是由 抛出的thread.join()。问题是,我搜索了 Stackexchange,但无法解释其他消息。我意识到问题可能出在小代码片段之外(应用程序相当大,所以我不包括它)。但即使是这样,我只想问第二条错误消息的含义及其可能的原因。

Dai*_*aki 5

在我的特定情况下,抛出异常是因为线程已经加入。加入前检查加入。