简体   繁体   中英

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?

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?

Also is std::future::wait_for equivalent in performance with 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?

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?

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):

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

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.

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. 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.

can you give example with std::future/promise to signal multiple times?

have a look at the toy example from shared_future

Also is std::future::wait_for equivalent in performance with 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. So as you can understand std::future::wait_for adds a very small performance overhead to std::condition_variable::wait_for

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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