我在理解为什么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;
}
};
Run Code Online (Sandbox Code Playgroud)
有问题的电话是pool.schedule_one_or_yield();。如果我错了,请纠正我,但它表明提交的任务最终将被安排执行。如果是这样,不应该让之前的每个调用都boost::async(pool, &sorter::do_sort, this, std::move(new_lower_chunk));隐式地安排已提交的任务吗?
我知道 boost executor API 是实验性的,但你知道为什么schedule_one_or_yield没有记录吗?
该函数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())
{
}
}
Run Code Online (Sandbox Code Playgroud)
第一个循环重复调用schedule_one_or_yield(),这很简单
void schedule_one_or_yield()
{
if ( ! try_executing_one())
{
this_thread::yield();
}
}
Run Code Online (Sandbox Code Playgroud)
目前的实施loop_executor::loop是
/**
* The main loop of the worker thread
*/
void loop()
{
while (execute_one(/*wait:*/true))
{
}
BOOST_ASSERT(closed());
while (try_executing_one())
{
}
}
Run Code Online (Sandbox Code Playgroud)
来源: https: //github.com/boostorg/thread/blob/develop/include/boost/thread/executors/loop_executor.hpp
示例中也将其删除user_scheduler,旧版本位于
https://github.com/mongodb/mongo/blob/master/src/third_party/boost-1.60.0/boost/thread/user_scheduler.hppschedule_one_or_yield()第 63行
没有的新版本schedule_one_or_yield()位于 https://github.com/boostorg/thread/blob/develop/example/user_scheduler.cpp
| 归档时间: |
|
| 查看次数: |
1498 次 |
| 最近记录: |