[MS] Sharing the result of a single Windows Runtime IAsyncOperation among multiple coroutines, part 3 - devamazonaws.blogspot.com
Last time, we wrote a coroutine function that cached the result of another coroutine , but it only cached successful calls. It didn't cache failures, so if the inner coroutine fails, the outer one will simply try again the next time it is called. But what if we want to call the inner coroutine only once and cache the failure, too? We now have three states: Never tried, tried with success (and cache the success result), and tried with failure (and cache the failure). We can represent that as a variant with three types, using std:: monostate to represent the "never tried" state.¹ struct Widget : WidgetT<Widget> { std::variant<std::monostate, winrt::Thing, std::exception_ptr> m_thing; wil::unique_event m_busy{ wil::EventOptions::Signaled }; // auto-reset, initially signaled IAsyncOperation<winrt::Thing> GetThingAsync() { auto lifetime = get_strong(); co_await winrt::resume_on_signal(m_busy.get()); auto not_busy ...