知识卡片

问题卡 · 5968

周期通知需要受锁保护的条件

理解等级:中级

Condition配合受同一锁保护的状态:等待方记住观察值并在while中等待其变化,通知方先改状态再notify_all。原PeriodicTimer每次异或翻转一位,慢线程若错过偶数次tick会再次见相同值,因此不保证每个周期都交付;需要计次数或补偿时应使用代数计数/排队协议。sleep循环也不等于无漂移的精确定时。

这张卡片看不懂?

已有反馈 · 0 / 10 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • · 查看原书依据

      last_flag == self._flag: self._cv.wait() # Example use of the timer ptimer = PeriodicTimer(5) ptimer.start() # Two threads that synchro

    参考来源

    - 位置:《Python Cookbook(第3版)中文版》12.2 判断线程是否已经启动(spine20;extracted/Python Cookbook(第3版)中文版/_epub-src/OEBPS/Text/part0018.xhtml;u211-ch12-009)。 - 结论依据:完整读取本条问题、方案、代码与讨论;结论按真实控制流和适用前提表达,额外边界明确为解释或推导,示例未执行。 - 原始内容:last_flag == self._flag: self._cv.wait() # Example use of the timer ptimer = PeriodicTimer(5) ptimer.start() # Two threads that synchro