实现搜索使用期货,异步和线程在C ++ 11 [英] Implement search using futures, async, and thread in C++11

查看:140
本文介绍了实现搜索使用期货,异步和线程在C ++ 11的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现在多线程的方式分支限界搜索。我特别想用异步包裹在每个分支的搜索要求,然后就等待,直到某个线程想出了答案,只是退出。 (理想情况下,我会想取消其他线程,但线程抵消不在标准)。下面是一些简单的code:

I would like to implement branch and bound search in a multithreaded manner. In particular, I want to use async to wrap the search calls at each branch, then just wait until some thread comes up with the answer, and just exit. (Ideally, I would want to cancel the other threads, but thread cancelling is not in the standard). Here is some simplified code :

#include <iostream>
#include <random>
#include <future>
#include <thread>

using namespace std;

mt19937 rng;
uniform_int_distribution<unsigned> random_binary(0, 1);

bool search() {
  return static_cast<bool>(random_binary(rng));
}

#define N 10000

int main()
{
  rng.seed(42);

  std::vector<future<bool>> tasks;

  for (unsigned i=0; i<N; ++i)
    tasks.push_back(async(launch::async, search));

  // Don't want to wait sequentially here.
  for (unsigned i=0; i<N; ++i) {
    tasks[i].wait();
    if (tasks[i].get()) {
      cout << "i = " << i << "\n";
      break;
    }
  }
  return 0;
}

搜索()是搜索功能。它根据是否找到了答案还是不返回真/假。我回一个随机的答案说明。但问题的关键是在for循环调用任务[I] .wait()。现在,我的任务完成顺序等候。相反,我想要做这样的事情:

search() is the search function. It returns true/false based on whether it found the answer or not. I return a random answer for illustration. But the crux of the problem is in the for loop that calls tasks[i].wait(). Right now, I am waiting sequentially for tasks to finish. Instead I want to do something like this :

auto x = wait_for_any(tasks.begin(), tasks.end());
x.get();
// cancel other threads.
// Profit?

什么是实现这一目标的好方法?

What is a good way to achieve this?

推荐答案

的std ::未来提供的 有效的() 功能,可以让你检查,如果结果可用不阻塞,所以你可以使用,例如:在繁忙的等待循环:

std::future provides a valid() function that lets you check if the result is available without blocking, so you can just use that, e.g. in a busy-wait loop:

std::future<bool>* res_future = 0;
for(size_t i = 0; ; i==tasks.size()?i=0:++i){
  // could add a timeout period to not completely busy-wait the CPU
  if(tasks[i].wait_for(std::chrono::seconds(0)) == std::future_status::ready){
    res = &tasks[i];
    break;
  }
}

bool res = res_future->get();

一个建议除了的std ::未来,使这样的工作变得简单,是一个。然后(func_obj)方法异步调用 func_obj 当结果是可用的,在那里你可以设置标志或什么的。

A proposed addition to std::future, to make tasks like this easier, is a .then(func_obj) method that asynchronously calls the func_obj when the result is available, where you could set a flag or something.

我遗憾的是不知道的方式来实现可能 wait_for_any 在上面比任何其他方式。 :/

I sadly don't know of a way to possibly implement wait_for_any in any other way than above. :/

template<class FwdIt>
std::future<bool> wait_for_any(FwdIt first, FwdIt last)
{
  return std::async([=]{
    for(FwdIt cur(first); ; cur==last?cur=first:++cur){
    // could add a timeout period to not completely busy-wait the CPU
    if(cur->wait_for(std::chrono::seconds(0)) == std::future_status::ready)
      return cur->get();
  });
}

主题的破坏通常是与合作消除完成。

Thread destruction is usually done with cooperative cancellation.

P上。小号:的std ::未来&LT; T&GT; ::得到()将自动等待()如果结果是不可用。

P. S.: std::future<T>::get() will automatically wait() if the result is not available.

这篇关于实现搜索使用期货,异步和线程在C ++ 11的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆