Note
作為本主題中範例程式碼的替代方案,您可以在 GitHub 上的 cpp-async 存放庫中找到適用於正式環境的工作完成來源實作版本原始碼。
本主題示範如何撰寫並使用自己的補全原始碼類別,類似 .NET 的 TaskCompletionSource。
completion_source範例的原始碼
以下列表中的程式碼僅作為範例提供。 它的目的是說明你如何寫出屬於自己的版本。 例如,對抵銷和誤差傳播的支持不在本範例範圍內。
#include <winrt/base.h>
#include <windows.h>
template <typename T>
struct completion_source
{
completion_source()
{
m_signal.attach(::CreateEvent(nullptr, true, false, nullptr));
}
void set(T const& value)
{
m_value = value;
::SetEvent(m_signal.get());
}
bool await_ready() const noexcept
{
return ::WaitForSingleObject(m_signal.get(), 0) == 0;
}
void await_suspend(std::experimental::coroutine_handle<> resume)
{
m_wait.attach(winrt::check_pointer(::CreateThreadpoolWait(callback, resume.address(), nullptr)));
::SetThreadpoolWait(m_wait.get(), m_signal.get(), nullptr);
}
T await_resume() const noexcept
{
return m_value;
}
private:
static void __stdcall callback(PTP_CALLBACK_INSTANCE, void* context, PTP_WAIT, TP_WAIT_RESULT) noexcept
{
std::experimental::coroutine_handle<>::from_address(context)();
}
struct wait_traits
{
using type = PTP_WAIT;
static void close(type value) noexcept
{
::CloseThreadpoolWait(value);
}
static constexpr type invalid() noexcept
{
return nullptr;
}
};
winrt::handle m_signal;
winrt::handle_type<wait_traits> m_wait;
T m_value{};
};
將完成處理交由另一個協程執行
本節展示 completion_source的一個使用案例。 根據Windows控制台應用程式(C++/WinRT)專案範本,在Visual Studio建立一個新專案,並將以下程式碼清單貼上(main.cpp根據前一節的清單擴充completion_source定義)。
// main.cpp
#include "pch.h"
#include <winrt/base.h>
#include <windows.h>
template <typename T>
struct completion_source
{
... // Paste the listing of completion_source here.
}
using namespace std::literals;
using namespace winrt;
using namespace Windows::Foundation;
fire_and_forget CompleteAfterFiveSecondsAsync(completion_source<bool>& completionSource)
{
co_await 5s;
completionSource.set(true);
}
IAsyncAction CompletionSourceExample1Async()
{
completion_source<bool> completionSource;
CompleteAfterFiveSecondsAsync(completionSource);
co_await completionSource;
}
int main()
{
auto asyncAction { CompletionSourceExample1Async() };
puts("waiting");
asyncAction.get();
puts("done");
}
將 completion_source 封裝在類別中,並回傳一個值
在接下來的例子中,使用一個簡單的 App 類別封裝一個 completion_source,並在完成後回傳一個值。 根據Windows控制台應用程式(C++/WinRT)專案範本,在Visual Studio建立一個新專案,並將以下程式碼清單貼上(main.cpp根據前一節的清單擴充completion_source定義)。
// main.cpp
#include "pch.h"
#include <winrt/base.h>
#include <windows.h>
template <typename T>
struct completion_source
{
... // Paste the listing of completion_source here.
}
using namespace std::literals;
using namespace winrt;
using namespace Windows::Foundation;
struct App
{
completion_source<winrt::hstring> m_completionSource;
IAsyncOperation<winrt::hstring> CompletionSourceExample2Async()
{
co_return co_await m_completionSource;
}
winrt::fire_and_forget CompleteAfterFiveSecondsAsync()
{
co_await 5s;
m_completionSource.set(L"Hello, World!");
}
};
int main()
{
App app;
auto asyncAction{ app.CompletionSourceExample2Async() };
app.CompleteAfterFiveSecondsAsync();
puts("waiting");
auto message = asyncAction.get();
printf("%ls\n", message.c_str());
}