简体   繁体   English

加入线程时异常

[英]Exception when joining threads

The following test program compiles with g++ -o test -pthread -std=c++11 test.cpp just fine: 以下测试程序可以使用g++ -o test -pthread -std=c++11 test.cpp

#include <iostream>
#include <vector>
#include <thread>
#include <chrono>

using namespace std;

void foo(int);

int main()
{
        vector<thread> threads(5);
        for (int i = 0; i < 5; i++)
        {
                threads.push_back(thread(foo, i));
        }

        for (vector<thread>::iterator iter = threads.begin(); iter != threads.end(); iter++)
        {
                iter->join();
        }
}

void foo(int id)
{
        cout << "thread " << id << " started.";
        this_thread::sleep_for(chrono::seconds(10));
        cout << "thread " << id << " terminated.";
}

However, when run, it gives output similar to this: 但是,在运行时,它会提供类似于以下的输出:

terminate called after throwing an instance of 'std::system_error'
  what():  Invalid argument
thread thread 2 started.1 started.thread 4 started.thread 3 started.thread 0 started.Aborted (core dumped)  

I have no idea where this error is coming from. 我不知道这个错误是从哪里来的。 From debugging, I know that the error happens as soon as I try to join the first thread. 通过调试,我知道该错误在我尝试加入第一个线程时立即发生。
http://www.cplusplus.com/reference/thread/thread/join/ states that join will only throw invalid_argument if the thread is not joinable. http://www.cplusplus.com/reference/thread/thread/join/指出,如果线程不可连接,则连接只会抛出invalid_argument
http://www.cplusplus.com/reference/thread/thread/joinable/ states that a thread is not joinable only if: http://www.cplusplus.com/reference/thread/thread/joinable/声明仅在以下情况下线程不可加入:

  • it was default-constructed. 它是默认构造的。
  • it has been moved from (either constructing another thread object, or assigning to it). 它已被移出(构造另一个线程对象或分配给它)。
  • either of its members join or detach has been called. 其成员之一join或detach已被调用。

As one can clearly see, the thread was not default-constructed. 可以清楚地看到,该线程不是默认构造的。
I am never overwriting the thread with another thread object. 我永远不会用另一个线程对象覆盖该线程。 The output of the program also shows that each thread is clearly running, as the "thread x started." 程序的输出还显示每个线程都在清晰地运行,如“线程x已启动”。 statements are reached. 语句已到达。
I am only calling join once for each thread. 我只为每个线程调用一次join。

If I put a 5-second pause between creation and joining, I get the same outout, so it's not a race issue. 如果在创建和加入之间放置5秒钟的停顿,我会得到相同的结果,因此这不是种族问题。 Although it is interesting that the output is still ordered in the same way. 尽管有趣的是,输出仍然以相同的方式排序。 Any ideas? 有任何想法吗?

You shouldn't presize the vector with 5 default-constructed threads, then ignore those while push_back -ing additional ones. 您不应该使用5个默认构造的线程来预先调整vector ,而在push_back添加其他线程时忽略它们。 Your join then tries to join the default-constructed threads and throws. 然后,您的join尝试联接默认构造的线程并引发。 Get rid of the (5) , or move it to a reserve call. 摆脱(5)或将其移至reserve电话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM