90

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?

2
  • 5
    You just pass them in: std::thread(func1, arg1, arg2, arg3);. Remember to join or detach it, though. Commented Dec 3, 2013 at 0:47
  • When you get an error message, include it. Commented Dec 3, 2013 at 14:05

5 Answers 5

97

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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 yeah I though your code should have compiled, even though it was technically incorrect
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
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.
@Darksaint2014 is storePose a member function of some object?
19

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.

Explanation (thanks @Holger Böhnke):

The reference must point to a copy-constructable object (copied when passing between threads) or use std::ref(yourParam) to explicitly request a shared reference.

3 Comments

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 ...
This solution also worked for me when I was trying to pass a non const reference to a struct as a parameter
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 stackoverflow.com/a/34078246/1614903 for more details.
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

2 Comments

This does not seem to be the cause of the issue.
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...."
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!!

Comments

-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.

1 Comment

"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 question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.