【问题标题】:Passing shared_ptr as an argument to thread function, results into an error将 shared_ptr 作为参数传递给线程函数,会导致错误
【发布时间】:2019-10-06 05:03:33
【问题描述】:

我正在尝试做类似以下的事情:

std::shared_ptr<asengine::PreCompileConfig> precompileconfigobj = std::make_shared<asengine::PreCompileConfig>();

std::thread rsync_t(&asengine::PreCompile::RunRsyncDb, precompileconfigobj));

我收到以下错误:

/usr/include/c++/4.8/functional:1697:61: error: no type named 'type' in 'class std::result_of<std::_Mem_fn<void (asengine::PreCompile::*)(const std::shared_ptr<asengine::PreCompileConfig>&)>(std::reference_wrapper<std::shared_ptr<asengine::PreCompileConfig> >)>'
   typedef typename result_of<_Callable(_Args...)>::type result_type;
                                                         ^
/usr/include/c++/4.8/functional:1727:9: error: no type named 'type' in 'class std::result_of<std::_Mem_fn<void (asengine::PreCompile::*)(const std::shared_ptr<asengine::PreCompileConfig>&)>(std::reference_wrapper<std::shared_ptr<asengine::PreCompileConfig> >)>'
     _M_invoke(_Index_tuple<_Indices...>)

经过简要研究,我发现线程函数需要参数对象的引用,而不是值。

std::thread rsync_t(&asengine::PreCompile::RunRsyncDb, std::ref(precompileconfigobj));

但是,它仍然不能解决问题。任何解决问题的帮助将不胜感激。

【问题讨论】:

  • 显示RunRsyncDb 方法的声明。我猜它是static 那么你不需要将precompileconfigobj 作为参数传递。
  • @rafix07 void RunRsyncDb(const std::shared_ptr& shptrprecompileconfigobj, const std::shared_ptr& slog);

标签: c++11 shared-ptr ref


【解决方案1】:

我想我解决了这个问题。我必须传递要调用线程函数的对象。

std::thread rsync_t(&asengine::PreCompile::RunRsyncDb, precompileobj, std::ref(precompileconfigobj));

【讨论】:

    猜你喜欢
    • 2013-01-10
    • 2013-10-29
    • 2022-11-25
    • 2017-06-24
    • 1970-01-01
    • 1970-01-01
    • 2020-11-14
    • 2013-08-02
    • 1970-01-01
    相关资源
    最近更新 更多