首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >理解boost executor示例

理解boost executor示例
EN

Stack Overflow用户
提问于 2018-10-07 12:41:22
回答 1查看 2K关注 0票数 5

我很难理解为什么在下面的示例中使用boost::basic_thread_pool executor的(无文档的)接口,这个接口是从boost文档本身获取的:

代码语言:javascript
复制
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是没有文档的吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-08 08:07:27

函数schedule_one_or_yield()被从boost的当前源代码中删除,因为它实现了繁忙的等待。这是在

https://github.com/boostorg/thread/issues/117

loop_executor::loop目前正在:

代码语言:javascript
复制
void loop()
{
  while (!closed())
  {
    schedule_one_or_yield();
  }
  while (try_executing_one())
  {
  }
}

第一个循环重复调用schedule_one_or_yield(),这是非常简单的

代码语言:javascript
复制
void schedule_one_or_yield()
{
    if ( ! try_executing_one())
    {
      this_thread::yield();
    }
}

当前is的loop_executor::loop实现

代码语言:javascript
复制
/**
     * 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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52688586

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档