我很难理解为什么在下面的示例中使用boost::basic_thread_pool executor的(无文档的)接口,这个接口是从boost文档本身获取的:
template<typename T>
struct sorter
{
boost::basic_thread_pool pool;
typedef std::list<T> return_type;
std::list<T> do_sort(std::list<T> chunk_data)
{
if(chunk_data.empty()) {
return chunk_data;
}
std::list<T> result;
result.splice(result.begin(),chunk_data, chunk_data.begin());
T const& partition_val=*result.begin();
typename std::list<T>::iterator divide_point =
std::partition(chunk_data.begin(), chunk_data.end(),
[&](T const& val){return val<partition_val;});
std::list<T> new_lower_chunk;
new_lower_chunk.splice(new_lower_chunk.end(), chunk_data,
chunk_data.begin(), divide_point);
boost::future<std::list<T> > new_lower =
boost::async(pool, &sorter::do_sort, this, std::move(new_lower_chunk));
std::list<T> new_higher(do_sort(chunk_data));
result.splice(result.end(),new_higher);
while(!new_lower.is_ready()) {
pool.schedule_one_or_yield();
}
result.splice(result.begin(),new_lower.get());
return result;
}
};正在讨论的呼叫是pool.schedule_one_or_yield();.如果我错了,请纠正我,但这意味着提交的任务最终会被安排执行。如果是这样的话,是否应该让以前对boost::async(pool, &sorter::do_sort, this, std::move(new_lower_chunk));的每次调用都已经隐式地安排了提交的任务?
我知道boost executor是实验性的,但是您知道为什么schedule_one_or_yield是没有文档的吗?
发布于 2018-10-08 08:07:27
函数schedule_one_or_yield()被从boost的当前源代码中删除,因为它实现了繁忙的等待。这是在
https://github.com/boostorg/thread/issues/117
loop_executor::loop目前正在:
void loop()
{
while (!closed())
{
schedule_one_or_yield();
}
while (try_executing_one())
{
}
}第一个循环重复调用schedule_one_or_yield(),这是非常简单的
void schedule_one_or_yield()
{
if ( ! try_executing_one())
{
this_thread::yield();
}
}当前is的loop_executor::loop实现
/**
* The main loop of the worker thread
*/
void loop()
{
while (execute_one(/*wait:*/true))
{
}
BOOST_ASSERT(closed());
while (try_executing_one())
{
}
}来源:executor.hpp
它也在示例user_scheduler中被删除,旧版本在
scheduler.hpp与第63行中的schedule_one_or_yield()
没有schedule_one_or_yield()的新版本在scheduler.cpp中
https://stackoverflow.com/questions/52688586
复制相似问题