理解boost executor示例

5

我对下面这个例子中的一个(未记录的)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 API是实验性的,但你知道为什么schedule_one_or_yield未记录在文档中吗?
1个回答

1

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

当前实现中,loop_executor::loop 是以下内容:
/**
     * The main loop of the worker thread
     */
    void loop()
    {
      while (execute_one(/*wait:*/true))
      {
      }
      BOOST_ASSERT(closed());
      while (try_executing_one())
      {
      }
}

来源: 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.hpp 第63行的 schedule_one_or_yield()

新版本没有 schedule_one_or_yield()https://github.com/boostorg/thread/blob/develop/example/user_scheduler.cpp


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接