等待多个期货? [英] Waiting for multiple futures?

查看:98
本文介绍了等待多个期货?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想运行相同类型的任务(工作线程),但一次只能执行一定数量的任务。当任务完成时,其结果是一个新任务的输入,然后可以开始。

I'd like to run tasks (worker threads) of the same type, but not more than a certain number of tasks at a time. When a task finishes, its result is an input for a new task which, then, can be started.

有什么好的方法来实现这个与异步/未来的范式在C ++ 11中?

Is there any good way to implement this with async/future paradigm in C++11?

乍一看,它看起来很简单,你只是用以下命令生成多个任务:

At first glance, it looks straight forward, you just spawn multiple tasks with:

std::future<T> result = std::async(...);

,然后运行 result.get()获得任务的异步结果。

and, then, run result.get() to get an async result of a task.

但是,这里的问题是未来的对象必须存储在某种队列中,并等待一个一。它是,虽然,可能重复对未来的对象一遍又一遍检查,如果它们中的任何一个是准备好,但由于不必要的CPU负载不是所期望的。

However, the problem here is that the future objects has to be stored in some sort of queue and be waited one by one. It is, though, possible to iterate over the future objects over and over again checking if any of them are ready, but it's not desired due to unnecessary CPU load.

它可能以某种方式等待来自给定集合的任何未来准备好并获得其结果?

Is it possible somehow to wait for any future from a given set to be ready and get its result?

我可以想到的唯一选项远远是一个没有任何异步/未来的老式方法。具体来说,生成多个工作线程并在每个线程的末尾将其结果推送到互斥锁保护队列中,通过条件变量通知等待线程,该队列已经更新了更多结果。

The only option I can think of so far is an old-school approach without any async/future. Specifically, spawning multiple worker threads and at the end of each thread push its result into a mutex-protected queue notifying the waiting thread via a condition variable that the queue has been updated with more results.

有什么其他更好的解决方案与async /未来可能?

Is there any other better solution with async/future possible?

推荐答案

p> C ++ 11中的线程支持只是第一次通过,而 std :: future 岩石,它不支持多个等待。

Thread support in C++11 was just a first pass, and while std::future rocks, it does not support multiple waiting as yet.

然而,你可以假装它相对效率不高。你最终为每个 std :: future 创建一个帮助线程(ouch,非常昂贵),然后收集他们的this future 准备就绪到同步的多生产者单消费者消息队列中,然后设置消费者任务,该消息任务调度给定 std :: future 准备好的事实。

You can fake it relatively inefficiently, however. You end up creating a helper thread for each std::future (ouch, very expensive), then gathering their "this future is ready" into a synchronized many-producer single-consumer message queue, then setting up a consumer task that dispatches the fact that a given std::future is ready.

此系统中的 std :: future 不会添加任何功能,他们准备好并把他们的结果粘在上面的队列会更有效率。如果你走这条路线,你可以编写符合 std :: async std :: thread ,并返回一个表示队列消息的 std :: future like对象。

The std::future in this system doesn't add much functionality, and having tasks that directly state that they are ready and sticks their result into the above queue would be more efficient. If you go this route, you could write wrapper that match the pattern of std::async or std::thread, and return a std::future like object that represents a queue message. This basically involves reimplementing a chunk of the the concurrency library.

如果你想保留 std :: future ,你可以创建 shared_future s,并且每个依赖任务依赖于 shared_future s的集合:ie,do it没有中央调度器。这不允许像abort / shutdown消息这样的东西,我认为这是一个健壮的多线程任务系统必不可少的。

If you want to stay with std::future, you could create shared_futures, and have each dependent task depend on the set of shared_futures: ie, do it without a central scheduler. This doesn't permit things like abort/shutdown messages, which I consider essential for a robust multi threaded task system.

最后,可以等待 C ++ 2x ,或者当并发TS被折叠时进入标准,为你解决问题。

Finally, you can wait for C++2x, or whenever the concurrency TS is folded into the standard, to solve the problem for you.

这篇关于等待多个期货?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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