Hi,
std::mutex is non-copyable. Therefore, the Foo class that contains it becomes non-copyable. You are passing a temporary Foo object to std::future, and you probably want to use the same object in all your threads. I suggest you could use std::ref to pass a reference of an existing object.
Here is the code:
#include <iostream>
#include <future>
struct Foo
{
int compute()
{
return 123;
}
// === UNCOMMENT THE LINE BELOW TO SHOW THE ERROR ===
std::mutex the_mutex;
};
int main()
{
Foo foo;
std::cout << "result: " << foo.compute() << std::endl;
std::future<int> f = std::async(std::launch::async, &Foo::compute, std::ref(foo));
std::cout << "result: " << f.get() << std::endl;
return 0;
}
Best Regards,
Jeanine Zhang
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.