02-race_conditions/

directory
v0.0.0-...-35bbffe Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2015 License: Apache-2.0

README

Race Conditions - Concurrency and Channels

A race condition 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 race condition.
  • Atomic functions and mutexes can provide the support we need.

http://blog.golang.org/race-detector

http://www.goinggo.net/2013/09/detecting-race-conditions-with-go.html

https://golang.org/doc/articles/race_detector.html

Documentation

Race Condition Diagram

Code Review

Race Condition (Go Playground)

Atomic Increments (Go Playground)

Atomic Store/Load (Go Playground)

Mutex (Go Playground)

Read/Write Mutex (Go Playground)

Exercises

Exercise 1

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

// https://play.golang.org/p/0C-mUZGUhE

// Program for an exercise to fix a race condition.
package main

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

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

// wg is used to wait for the program to finish.
var wg sync.WaitGroup

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

// main is the entry point for all Go programs.
func main() {
	// Add a count for each goroutine we will create.
	wg.Add(3)

	// Create three goroutines to generate random numbers.
	go random(10)
	go random(10)
	go random(10)

	// 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)
	}

	// Tell main we are done.
    wg.Done()
}

Template (Go Playground) | Answer (Go Playground)


Ardan Labs Ardan Studios GoingGo Blog


All material is licensed under the Apache License Version 2.0, January 2004.

Directories

Path Synopsis
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 the atomic package functions Store and Load to provide safe access to numeric types.
Sample program to show how to use the atomic package functions Store and Load 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
exercise1
Answer for exercise 1 of Race Conditions.
Answer for exercise 1 of Race Conditions.
template1
Fix the race condition in this program.
Fix the race condition in this program.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL