【问题标题】:access to shared_ptr in boost dispatcher在 boost 调度程序中访问 shared_ptr
【发布时间】:2014-02-05 20:42:48
【问题描述】:

我使用 boost 调度程序 (io_service) 异步执行“methodB”。在这个方法中,我想保留一个指向B类实例的指针,所以我使用了shared_ptr。但是在下面的例子中,我想知道在“methodA”的范围之后,指针是否仍然可以访问“methodB”,或者指针的 refcounter 是否会等于 0。

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 ???
}

非常感谢。

【问题讨论】:

    标签: boost boost-asio shared-ptr boost-thread dispatch


    【解决方案1】:

    以这种方式使用boost::bind 将复制参数,确保shared_ptr&lt;ClassB&gt; 保持在范围内。你所做的一切都很好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多