// Shared state created by std::async(). // Starts a new thread that runs a function and makes the shared state ready. template<typename _BoundFn, typename _Res> class__future_base::_Async_state_impl final : public __future_base::_Async_state_commonV2 { public: template<typename... _Args> explicit _Async_state_impl(_Args&&... __args) : _M_result(new _Result<_Res>()), _M_fn{{std::forward<_Args>(__args)...}} { _M_thread = std::thread{&_Async_state_impl::_M_run, this}; }
// Must not destroy _M_result and _M_fn until the thread finishes. // Call join() directly rather than through _M_join() because no other // thread can be referring to this state if it is being destroyed. ~_Async_state_impl() { if (_M_thread.joinable()) _M_thread.join(); }
private: void _M_run() { __try { _M_set_result(_S_task_setter(_M_result, _M_fn)); } __catch (const __cxxabiv1::__forced_unwind&) { // make the shared state ready on thread cancellation if (static_cast<bool>(_M_result)) this->_M_break_promise(std::move(_M_result)); __throw_exception_again; } }
template <class_Rx> class_Task_async_state : public _Packaged_state<_Rx()> { // class for managing associated synchronous state for asynchronous execution from async public: using _Mybase = _Packaged_state<_Rx()>; using _State_type = typename _Mybase::_State_type;
template <class_Fty2> _Task_async_state(_Fty2&& _Fnarg) : _Mybase(_STD forward<_Fty2>(_Fnarg)) { _Task = ::Concurrency::create_task([this]() { // do it now this->_Call_immediate(); });