Monday, May 2, 2016

Go race conditions

A data race occurs whenever two goroutines access the same variable concurrently and at least one of the accesses is a write.It follows from this definition that there are three ways to avoid adata race.

The first way is not to write the variable - load it beforehand and only give read access.

The second way to avoid a data race is to avoid accessing the variable from multiple goroutines - a monitor goroutine which selects on multiple channels.

Even when a variable cannot be confined to a single goroutine for its entire lifetime, confinement may still be a solution to the problem of concurrent access.For example, it's common to share a variable between goroutines in a pipeline by passing its address from one stage to the next over a channel.If each stage of the pipeline refrains from accessing the variable after sending it to the next stage, then all accesses to the variable are sequential.




No comments:

Blog Archive