#include <iostream>
#include <thread>
class MyClass {
public:
MyClass(int val) : val_(val) {}
int val_{0};
};
void threadFunction(MyClass myObj) {
// do something ...
}
int main() {
MyClass obj(42);
std::thread t1(threadFunction, obj);
std::thread t2([=]() {
threadFunction(obj);
});
t1.join();
t2.join();
return 0;
}
In thread t1
, I called threadfunction
directly whereas in thread t2
, I put the call of threadFunction
in a lambda.
Are these two ways of thread creation equivalent?
New contributor
Mathieu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.