线程作为class的成员变量
Thread as member variable of class
我想在某个class的成员变量中持有一个线程。以下代码片段显示了我想要实现的目标:
#include <iostream>
#include <thread>
#include <vector>
class Test {
public:
std::thread& t;
Test(std::thread&& rt) : t(rt) {}
};
int main()
{
std::vector<Test> tests;
{
std::thread t ([]{
std::cout << 1;
});
tests.push_back(Test(std::move(t)));
}
for(Test mytest : tests)
{
mytest.t.join();
}
}
代码将在 join() 行中断。错误是:
terminate called without an active exception
Aborted (core dumped)
为什么我不能通过 mytest.t 调用线程,而原始线程创建的范围是剩余的?
由于std::thread可移动但不可复制,你可以这样做:
class Test {
public:
std::thread t;
Test(std::thread&& rt) : t(std::move(rt)) {}
};
int main()
{
std::vector<Test> tests;
{
std::thread t([] {
std::cout << 1;
});
tests.push_back(Test(std::move(t)));
}
for (Test& mytest : tests)
{
mytest.t.join();
}
}
在您的 class 中,您有一个 引用 线程,而不是线程对象:
std::thread& t;
^
这意味着将发生以下序列:
{
std::thread t ([]{
std::cout << 1;
}); // 1. Thread is created.
tests.push_back(Test(std::move(t))); // 2. Reference to moved thread is taken
// and after move thread is destroyed.
// 3. Now the thread is destroyed,
// but not joined which will call `std::terminate`
// (Thanks @tkausl)
}
如果您 class std::thread t
此举会奏效。
正如@tkausl 所提到的,它是一个引用,{}
一旦超出范围并且您的引用不再有效,就会销毁线程对象。此外,您需要修改循环,使其不会创建原始 Test
对象的副本。修改后变成:
class Test {
public:
std::thread& t;
Test(std::thread&& rt) : t(rt) {}
};
int main()
{
std::vector<Test> tests;
std::thread t ([]{
std::cout << 1;
});
tests.push_back(Test(std::move(t)));
for(Test& mytest : tests)
{
mytest.t.join();
}
}
我想在某个class的成员变量中持有一个线程。以下代码片段显示了我想要实现的目标:
#include <iostream>
#include <thread>
#include <vector>
class Test {
public:
std::thread& t;
Test(std::thread&& rt) : t(rt) {}
};
int main()
{
std::vector<Test> tests;
{
std::thread t ([]{
std::cout << 1;
});
tests.push_back(Test(std::move(t)));
}
for(Test mytest : tests)
{
mytest.t.join();
}
}
代码将在 join() 行中断。错误是:
terminate called without an active exception
Aborted (core dumped)
为什么我不能通过 mytest.t 调用线程,而原始线程创建的范围是剩余的?
由于std::thread可移动但不可复制,你可以这样做:
class Test {
public:
std::thread t;
Test(std::thread&& rt) : t(std::move(rt)) {}
};
int main()
{
std::vector<Test> tests;
{
std::thread t([] {
std::cout << 1;
});
tests.push_back(Test(std::move(t)));
}
for (Test& mytest : tests)
{
mytest.t.join();
}
}
在您的 class 中,您有一个 引用 线程,而不是线程对象:
std::thread& t;
^
这意味着将发生以下序列:
{
std::thread t ([]{
std::cout << 1;
}); // 1. Thread is created.
tests.push_back(Test(std::move(t))); // 2. Reference to moved thread is taken
// and after move thread is destroyed.
// 3. Now the thread is destroyed,
// but not joined which will call `std::terminate`
// (Thanks @tkausl)
}
如果您 class std::thread t
此举会奏效。
正如@tkausl 所提到的,它是一个引用,{}
一旦超出范围并且您的引用不再有效,就会销毁线程对象。此外,您需要修改循环,使其不会创建原始 Test
对象的副本。修改后变成:
class Test {
public:
std::thread& t;
Test(std::thread&& rt) : t(rt) {}
};
int main()
{
std::vector<Test> tests;
std::thread t ([]{
std::cout << 1;
});
tests.push_back(Test(std::move(t)));
for(Test& mytest : tests)
{
mytest.t.join();
}
}