gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:37:50 30 4
gpt4 key购买 nike

遵循 boost::thread 关于 thread parameters 的教程我想出了以下代码片段(根据 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 )

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.

海湾合作委员会=============

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 构造函数。

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

例如:

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

关于c++ - 为什么 boost::thread 构造函数不接受 boost::thread::attributes 参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39981099/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com