简体   繁体   English

将多个参数传递给 std::thread

[英]Pass multiple arguments into std::thread

I'm asking the <thread> library in C++11 standard.我问的是 C++11 标准中的<thread>库。

Say you have a function like:假设你有一个类似的功能:

void func1(int a, int b, ObjA c, ObjB d){
    //blahblah implementation
}

int main(int argc, char* argv[]){
    std::thread(func1, /*what do do here??*/);
}

How do you pass in all of those arguments into the thread?您如何将所有这些参数传递到线程中? I tried listing the arguments like:我尝试列出如下参数:

std::thread(func1, a,b,c,d);

But it complains that there's no such constructor.但它抱怨没有这样的构造函数。 One way to get around this is defining a struct to package the arguments, but is there another way to do this?解决这个问题的一种方法是定义一个结构来打包参数,但还有另一种方法可以做到这一点吗?

You literally just pass them in std::thread(func1,a,b,c,d);您实际上只是将它们传递给std::thread(func1,a,b,c,d); that should have compiled if the objects existed, but it is wrong for another reason.如果对象存在,它应该已经编译,但由于另一个原因它是错误的。 Since there is no object created you cannot join or detach the thread and the program will not work correctly.由于没有创建对象,您无法加入或分离线程,程序将无法正常工作。 Since it is a temporary the destructor is immediately called, since the thread is not joined or detached yet std::terminate is called.由于它是临时的,因此立即调用析构函数,因为线程尚未加入或分离,但std::terminate被调用。 You could std::join or std::detach it before the temp is destroyed, like std::thread(func1,a,b,c,d).join();//or detach .您可以在临时销毁之前std::joinstd::detach它,例如std::thread(func1,a,b,c,d).join();//or detach

This is how it should be done.这是应该如何做的。

std::thread t(func1,a,b,c,d);
t.join();  

You could also detach the thread, read-up on threads if you don't know the difference between joining and detaching.如果您不知道加入和分离之间的区别,您也可以分离线程,阅读线程。

Had the same problem.有同样的问题。 I was passing a non-const reference of custom class and the constructor complained (some tuple template errors).我正在传递自定义类的非常量引用,并且构造函数抱怨(一些元组模板错误)。 Replaced the reference with pointer and it worked.用指针替换了引用并且它起作用了。

This problem can be solved by creating a class which can hold parameters of function.这个问题可以通过创建一个可以保存函数参数的类来解决。

Create a object of this class with argument to be pass.创建一个此类的对象,并带有要传递的参数。

then pass this object to thread.然后将此对象传递给线程。

function should also be modified accordingly.功能也应相应修改。

If you're getting this, you may have forgotten to put #include <thread> at the beginning of your file.如果你得到这个,你可能忘记把#include <thread>放在文件的开头。 OP's signature seems like it should work. OP 的签名似乎应该可以工作。

On Mac I tried this and it works:在 Mac 上我试过这个并且它有效:

#include <iostream>
#include <thread>

using namespace std;

string t1(int firstInput, int secondInput){
    cout << firstInput << " ,";
    cout << secondInput << " ,";

    return "finish thread t1";
}


int main()
    {   
        int userInput1;
        int userInput2;
        cout << "Set a number" << endl; 
        cin >> userInput1;
        cout << "Set second number"<< endl;
        cin >> userInput2;
        thread ThreadT1(t1, userInput1, userInput2 );
        ThreadT1.join();

    }

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

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