访问boost dispatcher中的共享\u ptr

访问boost dispatcher中的共享\u ptr,boost,boost-asio,shared-ptr,boost-thread,dispatch,Boost,Boost Asio,Shared Ptr,Boost Thread,Dispatch,我使用boost dispatcher(io_服务)异步执行“methodB”。在这个方法中,我想保留一个指向类B实例的指针,所以我使用shared_ptr。但在下面的示例中,我想知道在“methodA”的作用域之后,指针是否仍然可以访问到“methodB”,或者指针refcounter是否等于零 void ClassA::methodA(){ shared_ptr<ClassB> pointer(new ClassB); m_dispatcher.post(boost::b

我使用boost dispatcher(io_服务)异步执行“methodB”。在这个方法中,我想保留一个指向类B实例的指针,所以我使用shared_ptr。但在下面的示例中,我想知道在“methodA”的作用域之后,指针是否仍然可以访问到“methodB”,或者指针refcounter是否等于零

void ClassA::methodA(){
  shared_ptr<ClassB> pointer(new ClassB);
  m_dispatcher.post(boost::bind(&ClassA::methodB, this, pointer); // boost io_service
}

void ClassA::methodB(shared_ptr<ClassB> pointer){
  ClassB *p = pointer.get(); // access to pointer ???
}
void ClassA::methodA(){
共享ptr指针(新类别B);
m_dispatcher.post(boost::bind(&ClassA::methodB,this,指针);//boost io_服务
}
void ClassA::methodB(共享\u ptr指针){
ClassB*p=pointer.get();//访问指针???
}

非常感谢。

以这种方式使用
boost::bind
将复制参数,确保
共享的\u ptr
仍在范围内。您所做的一切都很好