When you construct a new thread the supplied function object is copied into the storage belonging to the newly created thread. I want to execute an object method in a new thread. The object should not be copied. So I pass shared_ptr of the object to std::thread constructor. How can I launch a new thread with std::shared_ptr() object? For example
class Foo {
public:
void operator()() {
// do something
}
};
int main() {
std::shared_ptr<Foo> foo_ptr(new Foo);
// I want to launch a foo_ptr() in a new thread
// Is this the correct way?
std::thread myThread(&Foo::operator(), foo_ptr.get());
myThread.join();
}