【发布时间】:2019-06-07 07:09:53
【问题描述】:
我想用一个包含不可复制成员的类来启动一个线程。为了与线程通信,我想在将对象移入线程之前从对象创建一个共享指针。
移动构造函数是否使共享指针无效?如果是这样,那么优雅的 C++ 方法是什么?一种解决方案是将 MessageClient 包装到共享指针中,但这会绕过移动构造函数。
class MessageClient : public std::enable_shared_from_this<MessageClient> {
private:
boost::asio::io_context io_context;
void send() {
// code
}
void operator() () {
// code
}
};
int main () {
MessageClient client;
auto pclient = client.shared_from_this();
std::thread thread(std::move(client));
pclient->send(); // terminate called after throwing an instance of 'std::bad_weak_ptr'
// what(): bad_weak_ptr
// Aborted (core dumped)
thread.join();
}
编辑
我得到了答案。我现在明白我错误地使用了enable_shared_from_this,但真正的答案是,没有办法解决不涉及将对象包装到另一个对象中的问题,例如智能指针或 lambda 函数(包装对象转化为函子)。就我个人而言,我发现第二种解决方案更简单,这就是我选择它的原因。
MessageClient client;
std::thread thread([&client](){client.run();});
client.send();
EDIT2
我找到了一个更明显的解决方案。如果我从对象创建引用,则不必包装它:
MessageClient client;
std::thread thread(std::ref(client));
client.send();
【问题讨论】:
-
shared_from_this只能在MessageClient的成员函数内部调用,前提是至少有一个shared_ptr实例对其进行管理。
标签: c++ multithreading shared-ptr move-constructor enable-shared-from-this