What is a race condition?
来源:互联网 发布:如何用c语言表白 编辑:程序博客网 时间:2024/05/21 10:46
A race condition occurs when two or more threads can access shared data and they try to change it at the same time. Because the thread scheduling algorithm can swap between threads at any time, you don't know the order in which the threads will attempt to access the shared data. Therefore, the result of the change in data is dependent on the thread scheduling algorithm, i.e. both threads are "racing" to access/change the data.
Problems often occur when one thread does a "check-then-act" (e.g. "check" if the value is X, then "act" to do something that depends on the value being X) and another thread does something to the value in between the "check" and the "act". E.g:
if (x == 5) // The "Check"{ y = x * 2; // The "Act" // If another thread changed x in between "if (x == 5)" and "y = x * 2" above, // y will not be equal to 10.}
The point being, y could be 10, or it could be anything, depending on whether another thread changed x in between the check and act. You have no real way of knowing.
In order to prevent race conditions from occurring, you would typically put a lock around the shared data to ensure only one thread can access the data at a time. This would mean something like this:
// Obtain lock for xif (x == 5){ y = x * 2; // Now, nothing can change x until the lock is released. // Therefore y = 10}// release lock for x
- What is a race condition?
- what is a data race?
- race condition
- Dereferencing a __weak pointer is not allowed due to possible null value caused by race condition, a
- What is a UUID?
- what is a lawyer
- What Is a Desision?
- What Is A Framework?
- what is a TFT?
- What is a THINKER?
- What is a disagreement
- What is a Closure?
- What is a Region?
- what is a map
- What is a FOURCC?
- What is a Program?
- what is a process?
- What is a FOURCC?
- 十张内涵图12月15日
- VB鼠标事件
- hadoop集群空间使用情况报告脚本
- Android应用开发以及设计思想深度剖析(5)
- MySQL写触发器,级联删除
- What is a race condition?
- ASP.Net获取错误信息
- 常用数据库连接方法
- ios--删除模拟器运行记录
- cassandra 数据模型简介
- dbunit中文教程
- arduino uno bootloader烧写方法
- Unable to open file 'lnk51ew_cc2530b.xcl'的解决方案
- XCode工程中 Project 和 Targets区别