c++ - 了解 boost 执行器示例

标签 c++ boost

我无法理解为什么在下面这个取自 boost documentation 的示例中使用了 boost::basic_thread_pool executor 的(未记录的)接口(interface)本身:

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 执行器 API 是实验性的,但你知道为什么 schedule_one_or_yield 没有记录吗?

最佳答案

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

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

loop_executor::loop is currently:

void loop()
{
  while (!closed())
  {
    schedule_one_or_yield();
  }
  while (try_executing_one())
  {
  }
}

The first loop repeatedly calls schedule_one_or_yield() which is simply

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

关于c++ - 了解 boost 执行器示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52688586/

相关文章:

c++ - 为什么要对地址运算符和引用声明符共享与符号 (&)?

c++ - std::stack<boost::shared_ptr<T>> 的深拷贝

regex - Boost regex用于解析字符串中的键/值对

c++ - 不支持 boost::filesystem::create_symlink

c++ - 使用 C++/boost::asio/libcurl 的简单代理 - 无法下载图像

c++ - 如何使用 boost 进行成员(member)访问复制

c++ - boost::unordered_map 缺少像 std::unordered_map 这样的 reserve()

c++ - 使用Boost Filesystem C++将具有特定扩展名的文件名保存在特定的文件夹中

c++ - 应用于三元时 decltype 的返回类型(?:) expression

c++ - 无论如何要从他们的 vtable 重建一些保存的类?