My question is does std::promise notify the associated std::future through using a std::condition_variable?
I search the source code of std::promise and found this website. But I didn't see std::promise has std::condition_variable in its member data.
std::condition_variable The condition_variable class is a synchronization primitive that can be used to block a thread, or multiple threads at the same time, until another thread both modifies a shared variable (the condition), and notifies the condition_variable .
The class template std::promise provides a facility to store a value or an exception that is later acquired asynchronously via a std::future object created by the std::promise object. Note that the std::promise object is meant to be used only once.
The class template std::future provides a mechanism to access the result of asynchronous operations: An asynchronous operation (created via std::async, std::packaged_task, or std::promise) can provide a std::future object to the creator of that asynchronous operation.
A condition variable is an object able to block the calling thread until notified to resume. It uses a unique_lock (over a mutex ) to lock the thread when one of its wait functions is called.
Here's an answer for libc++.
A search for condition_variable in <future> returned exactly one result:
// lines 531 -- 538
class _LIBCPP_TYPE_VIS _LIBCPP_AVAILABILITY_FUTURE __assoc_sub_state
: public __shared_count
{
protected:
exception_ptr __exception_;
mutable mutex __mut_;
mutable condition_variable __cv_;
unsigned __state_;
Here __assoc_sub_state is introduced. It is the base class for __assoc_state:
// lines 617 -- 619
template <class _Rp>
class _LIBCPP_AVAILABILITY_FUTURE __assoc_state
: public __assoc_sub_state
And finally, __assoc_state<_Rp>* is both a member of future<_Rp>:
// lines 1082 -- 1085
template <class _Rp>
class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE future
{
__assoc_state<_Rp>* __state_;
and a member of promise<_Rp>:
// lines 1360 -- 1363
template <class _Rp>
class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FUTURE promise
{
__assoc_state<_Rp>* __state_;
So yeah, libc++ std::promise internally uses a std::condition_variable to notify the associated std::future.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With