Data Races and Race Conditions
In this lesson, you will learn about data races which in most cases lead to race conditions.
We'll cover the following...
One of the reasons why concurrency is hard to achieve is because of data races.
Data Race
A data race happens when processes have to access the same variable concurrently i.e. one process reads from a memory location while another simultaneously writes to the exact same memory location.
The following function is an example of a data race:
Press + to interact
package mainimport "fmt"func main() {number := 0;go func(){number++ //reading and modifying the value of 'number'}()fmt.Println(number) //reading the value of 'number'}
We increment the value of the variable number
i.e. we first access the value, add 1
to it and then write the new value back to the memory. number++
...