Race condition
Updated: Sep 21st, 2023
经常看到说要避免竞态(Race)导致的危害,什么是竞态呢?竞态可能会带来什么问题呢?
什么是竞态
先来看看wiki的定义:
A race condition or race hazard is the condition of an electronics, software, or other system where the system’s substantive behavior is dependent on the sequence or timing of other uncontrollable events. It becomes a bug when one or more of the possible behaviors is undesirable.
写得挺清楚的,竞态有一个重要的条件:运行结果依赖于运行顺序、运行时间、时序等不受控制的条件,这意味着这些不受控制的条件会影响运行结果,更进一步,就是这些不受控制的条件,导致了程序发生了编程者没有预期的行为。
A race occurs when correctness of the program depends on one thread reaching point x before another thread reaches point y
竞态的几个典型例子
举几个竞态的例子,说明这些是如何导致程序的 bug。
- 两个线程同时给一个变量(原来为0)加1,在没有适当的同步原语时,两个线程不同的执行顺序,可能会导致该变量最终的结果为1或2。
> source : Replication(下):事务,一致性与共识 - Cubox
如何避免 Race Condition
- add some sync operations, such as lock. so ensure the atomity
一些前沿研究
- iGUARD[1]
- GPU的一些新型同步原语若使用不当,会导致Race。
参考资料
- wiki
- 关于几个概念:Race Condition vs. Data Race
参考文献
[1]
A.
K. Kamath and A. Basu, “iGUARD: In-GPU Advanced Race
Detection,” in Proceedings of the ACM SIGOPS 28th Symposium
on Operating Systems Principles, New York, NY, USA, 2021, pp.
49–65, doi: 10.1145/3477132.3483545
[Online]. Available: https://doi.org/10.1145/3477132.3483545.
[Accessed: 23-Nov-2021]
Notes mentioning this note
There are no notes linking to this note.