Lab4b 实验解析

tinyCoroLab4b 实验解析

⚠️tinyCoroLab 的实验强烈推荐实验者独自完成而非直接翻阅实验解析,否则这与读完题直接翻看参考答案无太大区别,实验解析仅供实验者参考。

本节将会以 tinyCoroLab 的官方实现tinyCoroarrow-up-right为例,为大家分析并完成 lab4b,请实验者预先下载 tinyCoro 的代码到本地。

git clone https://github.com/sakurs2/tinyCoro

打开include/coro/comp/latch.hpparrow-up-right并大致浏览代码结构。

📖lab4b 任务参考实现

🧑‍💻Task #1 - 实现 latch

在 event 的基础上实现 latch 是非常简单的,tinyCoro 的实现如下:

class latch
{
public:
    using event_t = event<>;
    latch(std::uint64_t count) noexcept : m_count(count), m_ev(count <= 0) {}
    latch(const latch&)                    = delete;
    latch(latch&&)                         = delete;
    auto operator=(const latch&) -> latch& = delete;
    auto operator=(latch&&) -> latch&      = delete;

    auto count_down() noexcept -> void
    {
        if (m_count.fetch_sub(1, std::memory_order::acq_rel) <= 1)
        {
            m_ev.set();
        }
    }

    auto wait() noexcept -> event_t::awaiter { return m_ev.wait(); }

private:
    std::atomic<std::int64_t> m_count;
    event_t                   m_ev;
};

latch 内部维护一个计数,由于是多线程所以采用原子变量实现,其对协程的挂载与恢复机制是完全通过 event 完成的,对于wait直接返回 event 的wait,而对于count_down,只需要在计数降至 0 后对 event 执行 set 就好了。

实验总结

  • 通过利用 event 实现 latch 并学会了如何有效利用现有资源简化新功能的添加并减少冗余代码逻辑

Last updated