# data_race/

directory
v0.0.0-...-86d22a7 Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Jul 17, 2017 License: Apache-2.0

### Data Races

A data race is when two or more goroutines attempt to read and write to the same resource at the same time. Race conditions can create bugs that totally appear random or can be never surface as they corrupt data. Atomic functions and mutexes are a way to synchronize the access of shared resources between goroutines.

### Notes

• Goroutines need to be coordinated and synchronized.
• When two or more goroutines attempt to access the same resource, we have a data race.
• Atomic functions and mutexes can provide the support we need.

### Cache Coherency and False Sharing

This content is provided by Scott Meyers from his talk in 2014 at Dive:

### Cache Coherency and False Sharing Notes

• Thread memory access matters.
• If your algorithm is not scaling look for false sharing problems.

Eliminate False Sharing

### Exercises

#### Exercise 1

Given the following program, use the race detector to find and correct the data race.

``````// https://play.golang.org/p/F5DCJTZ6Lm

// Fix the race condition in this program.
package main

import (
"fmt"
"math/rand"
"sync"
"time"
)

// numbers maintains a set of random numbers.
var numbers []int

// init is called prior to main.
func init() {
rand.Seed(time.Now().UnixNano())
}

// main is the entry point for the application.
func main() {
// Number of goroutines to use.
const grs = 3

// wg is used to manage concurrency.
var wg sync.WaitGroup

// Create three goroutines to generate random numbers.
for i := 0; i < grs; i++ {
go func() {
random(10)
wg.Done()
}()
}

// Wait for all the goroutines to finish.
wg.Wait()

// Display the set of random numbers.
for i, number := range numbers {
fmt.Println(i, number)
}
}

// random generates random numbers and stores them into a slice.
func random(amount int) {
// Generate as many random numbers as specified.
for i := 0; i < amount; i++ {
n := rand.Intn(100)
numbers = append(numbers, n)
}
}
``````

## Directories ¶

Path Synopsis
Sample program to show a more complicated race condition using an interface value.
Sample program to show a more complicated race condition using an interface value.
Sample program to show how to create race conditions in our programs.
Sample program to show how to create race conditions in our programs.
Sample program to show how to use the atomic package to provide safe access to numeric types.
Sample program to show how to use the atomic package to provide safe access to numeric types.
Sample program to show how to use a mutex to define critical sections of code that need synchronous access.
Sample program to show how to use a mutex to define critical sections of code that need synchronous access.
Sample program to show how to use a read/write mutex to define critical sections of code that needs synchronous access.
Sample program to show how to use a read/write mutex to define critical sections of code that needs synchronous access.
exercises
Answer for exercise 1 of Race Conditions.
Answer for exercise 1 of Race Conditions.
Fix the race condition in this program.
Fix the race condition in this program.