Thursday, April 30, 2020

What is a Data Race? Why Data Races are undesired? How Data Races can be avoided?

Q. What is a Data Race? Why Data Races are undesired? How Data Races can be prevented? (DEC 18, MAY 18, DEC 17) 10M
Q. Write a short note on Data Race. (MAY 19) 10M
Q. What is a Data Race? (MAY 17) 5M
Ans- Data race
A data race is a situation, in which at least two threads access a shared variable at the same time. At least on thread tries to modify the variable.

Races are typically caused by a human error: failure to follow a locking discipline or overall lack of such. Their exposure depends on non-determinism of the thread scheduler, which can interrupt threads at any time and cause their instructions to be interleaved in unpredictable ways. They can cause unpredictable crashes and data corruption. The worst property of data races is that they are notoriously hard to detect, reproduce, locate, and eliminate, which has earned them the name of Heisenbugs.

The only way to avoid data races is to synchronize access to all mutable data that is shared between threads. There are several ways to achieve this. In Go, you would normally use a channel or a lock. (Lower-lever mechanisms are available in the sync and sync/atomic packages.)
The preferred way to handle concurrent data access in Go is to use a channel to pass the actual data from one goroutine to the next. The motto is: “Don’t communicate by sharing memory; share memory by communicating.”

No comments:

Post a Comment