线程作为类的成员变量

问题描述 投票:1回答:3

我想在某个类的成员变量中保存一个线程。以下代码片段显示了我想要实现的目标:

#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调用该线程?

c++ c++11 stdthread
3个回答
6
投票

由于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();
    }

}

3
投票

在您的类中,您有一个线程的引用,而不是一个线程对象:

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)
}   

如果你让你的班级std::thread t移动将有效。


0
投票

正如@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();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.