76

I'm asking the <thread> library in C++11 standard.

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?

Prashant Kumar
  • 20,069
  • 14
  • 47
  • 63
turtlesoup
  • 3,188
  • 10
  • 36
  • 50

4 Answers4

88

You literally just pass them in 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. You could std::join or std::detach it before the temp is destroyed, like 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.

Guy Avraham
  • 3,482
  • 3
  • 38
  • 50
aaronman
  • 18,343
  • 7
  • 63
  • 78
  • 3
    Problem solved, I was an idiot for declaring 2 methods whose names are both func1, and compiler complained because it didn't know which one i was using. Your method does work. Thanks – turtlesoup Dec 03 '13 at 01:17
  • 1
    @turtlesoup yeah I though your code should have compiled, even though it was technically incorrect – aaronman Dec 03 '13 at 01:18
  • 9
    i am trying this but it keeps telling me that there are invalid arguments... `thread t(storePose, x_position, y_position, z_position, azimuth, att_pitch, att_roll, yaw, cam_pitch, cam_roll);` that is my thread, storePose is the func name with nine double params. It keeps saying there is no instance of constructor `std::thread::thread` – Darksaint2014 Nov 13 '14 at 22:01
  • Thanks for the explanation of multiple arguments and the inclusion of "you must join or detach". I am new to std::thread and this is good to know. – Wheezil Dec 09 '18 at 16:22
14

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.

Jiří
  • 415
  • 6
  • 16
  • It's what worked for me, I don't know why it was at -1 score... I wonder why there was this weird tuple error ... – MGamsby Feb 11 '20 at 22:22
  • This solution also worked for me when I was trying to pass a non const reference to a struct as a parameter – LuisDSS May 15 '23 at 07:17
0

If your error message says

error: no matching constructor for initialization of 'std::thread'

then it's likely because you forgot to specify the C++ standard to be 11. For g++ compiler:

g++ std=c++11 main.cpp -o main
-1

If you're getting this, you may have forgotten to put #include <thread> at the beginning of your file. OP's signature seems like it should work.

pixelpax
  • 1,435
  • 13
  • 22
  • 3
    "you may have forgotten to put `#include `" OP said that the error was that there was no such constructor, not that `std::thread` was not declared. Just pointing this out, because there is already an answer to the question – H-005 Jan 25 '21 at 16:32