我需要经常做这样的事情:
AsyncOperation * pAsyncOperation = new AsyncOperation();
auto bindOperation = std::bind(&AsyncOperation::operator(), std::ref(*pAsyncOperation));
std::thread thread(bindOperation );
thread.join();
AsyncOperation作为任何自定义类实现(operator() 也称为函数对象)。
是否可以指示std::bind使用 astd::shared_ptr而不是 a std::ref?这将防止内存泄漏,而我不需要保持对 的引用pAsyncOperation,并且会在线程结束时自动删除AsyncOperation,即这个异步任务的结束。
编辑:我并不总是可以访问 std::thread,线程库可以是 boost::thread 甚至任何其他平台相关线程。因此,无法访问 std::async。
我的主要问题是在 std::bind 中有所有权的概念。