69

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
  • 17,553
  • 14
  • 47
  • 63
turtlesoup
  • 2,908
  • 9
  • 33
  • 49

4 Answers4

80

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,156
  • 3
  • 36
  • 46
aaronman
  • 17,836
  • 6
  • 58
  • 78
  • 2
    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
  • 8
    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
11

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ří
  • 340
  • 4
  • 14
  • 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
-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,302
  • 12
  • 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
-2

On Mac I tried this and it works:

#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();

    }
sq.ashkan
  • 1
  • 1