Lab4c 实验解析
tinyCoroLab4c 实验解析
git clone https://github.com/sakurs2/tinyCoro📖lab4c 任务参考实现
🧑💻Task #1 - 实现 wait_group
class wait_group
{
public:
struct awaiter
{
awaiter(context& ctx, wait_group& wg) noexcept : m_ctx(ctx), m_wg(wg) {}
constexpr auto await_ready() noexcept -> bool { return false; }
auto await_suspend(std::coroutine_handle<> handle) noexcept -> bool;
auto await_resume() noexcept -> void;
auto resume() noexcept -> void;
context& m_ctx; // 绑定的 context
wait_group& m_wg; // 绑定的 wait_group
awaiter* m_next{nullptr}; // suspend awaiter 链表的 next 指针
std::coroutine_handle<> m_await_coro{nullptr}; // 待 resume 的协程句柄
};
explicit wait_group(int count = 0) noexcept : m_count(count) {}
auto add(int count) noexcept -> void;
auto done() noexcept -> void;
auto wait() noexcept -> awaiter;
private:
friend awaiter;
std::atomic<int32_t> m_count; // 保存计数
std::atomic<awaiter_ptr> m_state; // 与 event 的 m_state 功能一致
};实验总结
Last updated