知识卡片

普通读书笔记卡 · 2320.m.2

并发收益依赖状态隔离

理解等级:高级

并发让一个任务等待时其他任务继续,适合多核或I/O等待,但共享可变状态会发生交错竞态。两个线程各执行赋值与自增,结果可能因顺序不同而变化。必须识别临界区,用锁顺序访问或把状态按线程分开;增加线程数量不能替代共享状态设计,偶发竞态也不容易靠普通重复测试发现。

这张卡片看不懂?

已有反馈 · 0 / 10 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • 性能 · 查看原书依据

      As our example shows, race conditions can occur when two threads of control are present and there is shared state. The management of concurrency frequently comes down to managing how state is shared. One technique for preventing race conditions is to use locks to enforce sequential access to state. Another technique is to partition the state based on the thread executing a portion of code. That is, if we have two instances of x, x is not shared by the two threads and no race condition will occur.

    参考来源

    - 位置:《Software Architecture in Practice, 4th Edition》第09章《性能》(来源项目:book-sources;源文件:extracted/Software Architecture in Practice, 4th Edition/_epub-src/ch09.html) - 结论依据:所引连续原文给出本卡机制或取舍;条件与示例结合本节已读上下文解释。 - 原始内容:As our example shows, race conditions can occur when two threads of control are present and there is shared state. The management of concurrency frequently comes down to managing how state is shared. One technique for preventing race conditions is to use locks to enforce sequential access to state. Another technique is to partition the state based on the thread executing a portion of code. That is, if we have two instances of x, x is not shared by the two threads and no race condition will occur.