【问题标题】:Encapsulate thread function with boost bind用 boost bind 封装线程函数
【发布时间】:2011-12-07 12:35:43
【问题描述】:

我目前正在创建这样的 boost::threads:

  boost::thread m_myThread; //member variable
  //...
  m_myThread = boost::thread(boost::bind(&MyClass::myThreadFunction, this));

这将启动一个执行成员函数“myThreadFunction”的线程。

我想封装一个新线程的开始并使用类似的东西

boost::thread m_myThread; //member variable
//...
startAThread(&m_myThread, boost::bind(&MyClass::myThreadFunction, this), 123 /*some int argument*/);

我认为我可以像这样实现“startAThread”:

namespace internal
{
    template <typename Callable>
    void threadhelper(Callable func, int x)
    {
        doSomething(x);
        func();
    }
}
template <typename Callable>
void startAThread(boost::thread* threadToCreate, Callable func, int x)
{

    *threadToCreate = boost::thread(boost::bind(internal::threadhelper<Callable>, func, x));
}

但是,这无法编译

usr/include/boost/bind/bind.hpp: In member function 'void boost::_bi::list2<A1, A2>::operator()(boost::_bi::type<void>, F&, A&, int) [with F = void (*)(boost::_bi::bind_t<void, boost::_mfi::mf0<void, MyClass>, boost::_bi::list1<boost::_bi::value<MyClass*> > >, int), A = boost::_bi::list0, A1 = boost::_bi::bind_t<void, boost::_mfi::mf0<void, MyClass>, boost::_bi::list1<boost::_bi::value<MyClass*> > >, A2 = boost::_bi::value<int>]':
usr/include/boost/bind/bind_template.hpp:20:   instantiated from 'typename boost::_bi::result_traits<R, F>::type boost::_bi::bind_t<R, F, L>::operator()() [with R = void, F = void (*)(boost::_bi::bind_t<void, boost::_mfi::mf0<void, MyClass>, boost::_bi::list1<boost::_bi::value<MyClass*> > >, int), L = boost::_bi::list2<boost::_bi::bind_t<void, boost::_mfi::mf0<void, MyClass>, boost::_bi::list1<boost::_bi::value<MyClass*> > >, boost::_bi::value<int> >]'
usr/include/boost/thread/detail/thread.hpp:61:   instantiated from 'void boost::detail::thread_data<F>::run() [with F = boost::_bi::bind_t<void, void (*)(boost::_bi::bind_t<void, boost::_mfi::mf0<void, MyClass>, boost::_bi::list1<boost::_bi::value<MyClass*> > >, int), boost::_bi::list2<boost::_bi::bind_t<void, boost::_mfi::mf0<void, MyClass>, boost::_bi::list1<boost::_bi::value<MyClass*> > >, boost::_bi::value<int> > >]'
MyClass.cpp:160:   instantiated from here
usr/include/boost/bind/bind.hpp:313: error: conversion from 'void' to non-scalar type 'boost::_bi::bind_t<void, boost::_mfi::mf0<void, MyClass>, boost::_bi::list1<boost::_bi::value<MyClass*> > >' requested

这个错误是什么意思?代码有什么问题?

非常感谢!

【问题讨论】:

  • 您还应该添加对createThreadWithPriority 的调用(或者是上面对startAThread 的调用?)- 一个简单的错误是您没有将funccreateThreadWithPriorityinternal::threadhelper
  • 当然,你是对的。这是在准备发布代码时犯的两个错误。现在它应该对应于我实际拥有的代码。
  • 如果您询问您遇到的编译器问题,您应该始终发布复制问题的代码,并尝试帮助其他人尽可能多地阅读错误消息。特别是,代码中没有说明 MyClass.cpp 第 160 行的位置。 (我假设在您指向的错误之前没有错误)。我不知道即使有了这些信息,这是否可以很容易地回答,所以我会尝试创建一个与您的代码尽可能相似的测试并让它以同样的方式失败(从删除 boost::thread 开始从方程)。

标签: c++ boost boost-thread boost-bind


【解决方案1】:

试试

namespace internal
{
    template <typename Callable>
    void threadhelper(Callable func, int x)
    {
        doSomething(x);
        func();
    }
}
template <typename Callable>
void startAThread(boost::thread& threadToCreate, Callable func, int x) {        
    threadToCreate = boost::thread(boost::bind(&internal::threadhelper<Callable>, func, x));
}

我也使用了引用而不是指针。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-11
    • 2013-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-14
    • 1970-01-01
    相关资源
    最近更新 更多