简体   繁体   English

std::promise/std::future 对比 std::condition_variable in C++

[英]std::promise/std::future vs std::condition_variable in C++

Signalling between threads can be achieved with std::promise/future or with good old condition variables, can some one provide examples/use case where 1 would be a better choice over other?线程之间的信号可以通过 std::promise/future 或良好的旧条件变量来实现,有人可以提供示例/用例,其中 1 比其他更好的选择吗?

I know that CVs could be used to signal multiple times between threads, can you give example with std::future/promise to signal multiple times?我知道 CV 可用于在线程之间多次发出信号,您能否举例说明 std::future/promise 多次发出信号?

Also is std::future::wait_for equivalent in performance with std::condition_variable::wait? std::future::wait_for 的性能是否与 std::condition_variable::wait 等效? Lets say i need to wait on multiple futures in a queue as a consumer, does it make sense to go through each of them and check if they are ready like below?假设我需要作为消费者在队列中等待多个期货,通过每个期货 go 并检查它们是否准备就绪是否有意义,如下所示?

for(auto it = activeFutures.begin(); it!= activeFutures.end();) {
            if(it->valid() && it->wait_for(std::chrono::milliseconds(1)) == std::future_status::ready) {
                Printer::print(std::string("+++ Value " + std::to_string(it->get()->getBalance())));
                activeFutures.erase(it);

            } else {
                ++it;
            }
        }

can some one provide examples/use case where 1 would be a better choice over other?有人可以提供示例/用例,其中 1 比其他选择更好吗?

These are 2 different tools of the standard library.这是标准库的两种不同工具。 In order to give an example where 1 would be better over the other you'd have to come up with a scenario where both tools are a good fit.为了给出一个比另一个更好的例子,你必须想出一个两种工具都适合的场景。 However, these are different levels of abstractions to what they do and what they are good for.但是,这些是对它们的作用和用途的不同抽象层次。

from cppreference (emphasis mine):来自 cppreference(强调我的):

Condition variables条件变量

A condition variable is a synchronization primitive that allows multiple threads to communicate with each other.条件变量是允许多个线程相互通信的同步原语 It allows some number of threads to wait (possibly with a timeout) for notification from another thread that they may proceed.它允许一定数量的线程等待(可能超时)来自另一个线程的通知,它们可能会继续。 A condition variable is always associated with a mutex.条件变量总是与互斥锁相关联。

Futures期货

The standard library provides facilities to obtain values that are returned and to catch exceptions that are thrown by asynchronous tasks (ie functions launched in separate threads).标准库提供了获取返回值和捕获由异步任务(即在单独线程中启动的函数)抛出的异常的工具。 These values are communicated in a shared state, in which the asynchronous task may write its return value or store an exception, and which may be examined, waited for, and otherwise manipulated by other threads that hold instances of std::future or std::shared_future that reference这些值在共享的 state 中进行通信,异步任务可以在其中写入其返回值或存储异常,并且可以检查、等待或由持有std::future 或 std 实例的其他线程进行操作: :shared_future即引用

As you can see, a condition variable is a synchronization primitive whereas a future is a facility used to communicate results of asynchronous tasks.如您所见,条件变量是同步原语,而未来是用于传达异步任务结果的工具。

The condition variable can be used in a variety of scenarios where you need to synchronizes multiple threads, however you would typically use a std::future when you have tasks/jobs/work to do and you need it done without interrupting your main flow, aka asynchronously.条件变量可用于需要同步多个线程的各种场景,但是当您有任务/工作/工作要做并且需要在不中断主流程的情况下完成时,您通常会使用std::future ,又名异步。

so in my opinion a good example where you would use a future + promise is when you need to run a long running calculation and get/wait_for the result at a later point of time.所以在我看来,一个很好的例子就是当你需要运行一个长时间运行的计算并在稍后的时间点获取/等待结果时,你会使用一个 future + promise 。 In comparison to a condition variable, where you would have had to basically implement std::future + std::promise on your own, possibly using std::condition_variable somewhere internally.与条件变量相比,您基本上必须自己实现std::future + std::promise ,可能在内部某处使用std::condition_variable

can you give example with std::future/promise to signal multiple times?你能举例说明 std::future/promise 发出多次信号吗?

have a look at the toy example from shared_future看看来自shared_future的玩具示例

Also is std::future::wait_for equivalent in performance with std::condition_variable::wait? std::future::wait_for 的性能是否与 std::condition_variable::wait 等效?

well, GCC's implementation of std::future::wait_for uses std::condition_variable::wait_for which correlates with my explanation of the difference between the two.好吧,GCC 的std::future::wait_for实现使用std::condition_variable::wait_for这与我对两者之间差异的解释相关。 So as you can understand std::future::wait_for adds a very small performance overhead to std::condition_variable::wait_for所以你可以理解std::future::wait_for增加了一个非常小的性能开销到std::condition_variable::wait_for

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM