为什么boost :: thread构造函数不接受boost :: thread :: attributes参数?

时间:2016-10-11 15:33:20

标签: c++ multithreading boost boost-thread

关于thread parametersboost::thread教程,我想出了以下代码段(根据推荐手册应该有效):

#include <boost/thread.hpp>

void WorkerThread(int i)
{
  (void)i;
}

int main(int argc, char** argv)
{
  boost::thread::attributes attrs;
  attrs.set_stack_size(4096*2);
  boost::thread th(attrs, WorkerThread, 10);
  th.join();
}

提供以下输出(来自http://coliru.stacked-crooked.com/a/f5710518cc527147

clang =============

In file included from main.cpp:1:
In file included from /usr/local/include/boost/thread.hpp:13:
In file included from /usr/local/include/boost/thread/thread.hpp:12:
In file included from /usr/local/include/boost/thread/thread_only.hpp:22:
In file included from /usr/local/include/boost/thread/detail/thread.hpp:30:
In file included from /usr/local/include/boost/bind.hpp:22:
/usr/local/include/boost/bind/bind.hpp:319:9: error: type 'boost::thread_attributes' does not provide a call operator
        unwrapper<F>::unwrap(f, 0)(a[base_type::a1_], a[base_type::a2_]);
        ^~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/local/include/boost/bind/bind.hpp:1222:16: note: in instantiation of function template specialization 'boost::_bi::list2<boost::_bi::value<void (*)(int)>, boost::_bi::value<int> >::operator()<boost::thread_attributes, boost::_bi::list0>' requested here
        return l_( type<result_type>(), f_, a, 0 );
               ^
/usr/local/include/boost/thread/detail/thread.hpp:116:17: note: in instantiation of member function 'boost::_bi::bind_t<void, boost::thread_attributes, boost::_bi::list2<boost::_bi::value<void (*)(int)>, boost::_bi::value<int> > >::operator()' requested here
                f();
                ^
/usr/local/include/boost/thread/detail/thread.hpp:96:15: note: in instantiation of member function 'boost::detail::thread_data<boost::_bi::bind_t<void, boost::thread_attributes, boost::_bi::list2<boost::_bi::value<void (*)(int)>, boost::_bi::value<int> > > >::run' requested here
              thread_data(BOOST_THREAD_RV_REF(F) f_):
              ^
/usr/local/include/boost/thread/pthread/thread_heap_alloc.hpp:24:24: note: in instantiation of member function 'boost::detail::thread_data<boost::_bi::bind_t<void, boost::thread_attributes, boost::_bi::list2<boost::_bi::value<void (*)(int)>, boost::_bi::value<int> > > >::thread_data' requested here
            return new T(static_cast<A1&&>(a1));
                       ^
/usr/local/include/boost/thread/detail/thread.hpp:211:52: note: in instantiation of function template specialization 'boost::detail::heap_new<boost::detail::thread_data<boost::_bi::bind_t<void, boost::thread_attributes, boost::_bi::list2<boost::_bi::value<void (*)(int)>, boost::_bi::value<int> > > >, boost::_bi::bind_t<void, boost::thread_attributes, boost::_bi::list2<boost::_bi::value<void (*)(int)>, boost::_bi::value<int> > > >' requested here
            return detail::thread_data_ptr(detail::heap_new<detail::thread_data<typename boost::remove_reference<F>::type> >(
                                                   ^
/usr/local/include/boost/thread/detail/thread.hpp:397:25: note: in instantiation of function template specialization 'boost::thread::make_thread_info<boost::_bi::bind_t<void, boost::thread_attributes, boost::_bi::list2<boost::_bi::value<void (*)(int)>, boost::_bi::value<int> > > >' requested here
            thread_info(make_thread_info(boost::bind(boost::type<void>(),f,a1,a2)))
                        ^
main.cpp:12:21: note: in instantiation of function template specialization 'boost::thread::thread<boost::thread_attributes, void (*)(int), int>' requested here
      boost::thread th(attrs, WorkerThread, 10);
                    ^
1 error generated.

gcc =============

In file included from /usr/local/include/boost/bind.hpp:22:0,
                 from /usr/local/include/boost/thread/detail/thread.hpp:30,
                 from /usr/local/include/boost/thread/thread_only.hpp:22,
                 from /usr/local/include/boost/thread/thread.hpp:12,
                 from /usr/local/include/boost/thread.hpp:13,
                 from main.cpp:1:
/usr/local/include/boost/bind/bind.hpp: In instantiation of 'void boost::_bi::list2<A1, A2>::operator()(boost::_bi::type<void>, F&, A&, int) [with F = boost::thread_attributes; A = boost::_bi::list0; A1 = boost::_bi::value<void (*)(int)>; A2 = boost::_bi::value<int>]':
/usr/local/include/boost/bind/bind.hpp:1222:50:   required from 'boost::_bi::bind_t<R, F, L>::result_type boost::_bi::bind_t<R, F, L>::operator()() [with R = void; F = boost::thread_attributes; L = boost::_bi::list2<boost::_bi::value<void (*)(int)>, boost::_bi::value<int> >; boost::_bi::bind_t<R, F, L>::result_type = void]'
/usr/local/include/boost/thread/detail/thread.hpp:116:17:   required from 'void boost::detail::thread_data<F>::run() [with F = boost::_bi::bind_t<void, boost::thread_attributes, boost::_bi::list2<boost::_bi::value<void (*)(int)>, boost::_bi::value<int> > >]'
main.cpp:14:5:   required from here
/usr/local/include/boost/bind/bind.hpp:319:35: error: no match for call to '(boost::thread_attributes) (void (*&)(int), int&)'
         unwrapper<F>::unwrap(f, 0)(a[base_type::a1_], a[base_type::a2_]);
         ~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

似乎是什么问题?

1 个答案:

答案 0 :(得分:1)

它调用错误的boost::thread构造函数。

The constructor that accepts the attributes只接受不带参数的callable(线程函数)。因此,您必须使用boost::bindstd::bind或C ++ 11 lambda表达式自己创建一个可调用对象。

E.g:

boost::thread th(attrs, boost::bind(WorkerThread, 10));