Pass multiple arguments into std::thread
Asked Answered
S

5

87

I'm asking about 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 std::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?

Stabilize answered 3/12, 2013 at 0:46 Comment(2)
You just pass them in: std::thread(func1, arg1, arg2, arg3);. Remember to join or detach it, though.Rothberg
When you get an error message, include it.Preconscious
G
95

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.

Gough answered 3/12, 2013 at 0:54 Comment(5)
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. ThanksStabilize
@turtlesoup yeah I though your code should have compiled, even though it was technically incorrectGough
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::threadAware
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.Infantine
@Aware is storePose a member function of some object?Roughdry
S
16

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.

Susette answered 26/4, 2019 at 19:15 Comment(3)
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 ...Lhary
This solution also worked for me when I was trying to pass a non const reference to a struct as a parameterMclaurin
If you want to pass a reference parameter, you have to explicitly wrap it into std::ref(yourParam). The same holds true, if your functor is not copy contructable. See https://mcmap.net/q/194085/-passing-object-by-reference-to-std-thread-in-c-11 for more details.Keciakeck
N
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
Nordine answered 18/2, 2023 at 10:44 Comment(2)
This does not seem to be the cause of the issue.Biggin
Well, the OP didn't exactly say what the error message was. Which is why I prefaced my answer with an "If your error message says...."Nordine
P
0

In my case I just changed the function name and error gone.

Before: thread(merge, first_item, mid_item, last_item).join();

After: thread(doMerge, first_item, mid_item, last_item).join();

And error gone. Yes it's freaking!!

Petula answered 29/4 at 13:24 Comment(0)
E
-3

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.

Electrothermal answered 18/9, 2017 at 0:20 Comment(1)
"you may have forgotten to put #include <thread>" 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 questionHufford

© 2022 - 2024 — McMap. All rights reserved.