Patryk Patryk - 1 month ago 18
C++ Question

Why boost::thread constructor doesn't accept boost::thread::attributes parameter?

Following

boost::thread
's tutorial on thread parameters I came up with the following snippet (which according to boost's manual should work):

#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();
}


which gives the following output (from 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_]);
~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


what seems to be the problem?

Answer

It calls a wrong boost::thread constructor.

The constructor that accepts the attributes only accepts the callable (the thread function) without arguments. Hence, you have to create a callable object yourself with boost::bind, std::bind or C++11 lambda expression.

E.g.:

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