为什么 boost::thread 构造函数不接受 boost::thread::attributes 参数?
Why boost::thread constructor doesn't accept boost::thread::attributes parameter?
在boost::thread
关于线程参数的教程之后,我提出了以下代码片段(根据boost的手册应该工作):
#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)
叮当声 =============
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_]);
~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
有什么问题吗?
调用错误的boost::thread
构造函数
接受属性的构造函数只接受不带参数的可调用对象(线程函数)。因此,您必须使用boost::bind
, std::bind
或c++ 11 lambda表达式创建一个可调用的对象。
例如:
boost::thread th(attrs, boost::bind(WorkerThread, 10));
相关文章:
- 无法将参数传递给 boost::thread 构造函数
- 如何使用boost.thread运行多个接收器,每个线程应该分配给每个接收器?
- 使用 boost::thread 运行函子,但析构函数被错误地调用
- thread_specific_pointer可以在哪些平台上工作而无需 boost::thread?
- 将 boost::thread 与 C++11 std::mutex 混合使用是否安全?
- 同时使用 boost.python 和 boost.thread 时出现编译错误
- Boost.thread代码在Ubuntu和Windows中表现出不同的行为
- Pointer to boost::thread
- Linux上多核计算的低性能(openMP、boost::thread等)
- boost - thread.join() halts the ui
- 将基类的引用传递给 boost::thread 并在派生类中调用虚函数是否有效
- Difference between boost::thread and std::thread
- std::thread是boost::thread的替代品吗?
- 将 boost/thread headers 与 clang++ (Windows) 一起使用时出现问题
- 在linux下使用boost::thread创建一个boost::asio工作线程
- 如何在Windows上的CodeBlocks中构建和配置boost.thread
- C++ ReadConsoleInput 不适用于 boost::thread
- Boost.Thread超时等待
- 如何使用boost::thread创建线程池
- 使用std::vector和boost::thread进行内存管理