Created
September 24, 2019 00:18
-
-
Save brydavis/9513a23bc56dfaa6cbf9fd13388f531c to your computer and use it in GitHub Desktop.
Race condition in Go (see `letter` variable)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"sync" | |
) | |
const numThreads = 5 | |
var ch chan string | |
var wg sync.WaitGroup | |
var letter string | |
func main() { | |
ch = make(chan string, numThreads) | |
// Establish connection to source | |
// and destination database | |
// Data generator | |
wg.Add(1) | |
go generate() | |
// Worker threads | |
workers() | |
wg.Wait() | |
close(ch) | |
fmt.Println(letter) | |
} | |
func generate() { | |
/* For as long as we have data in database, | |
- Fetch the data in batch | |
- Delegate the task to workers via channel | |
*/ | |
// Simulating database access delay | |
// time.Sleep(300 * time.Millisecond) | |
data := []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"} | |
wg.Add(len(data)) | |
for i := range data { | |
// Data process delay | |
// time.Sleep(100 * time.Millisecond) | |
ch <- data[i] | |
} | |
// No more data to work on, notify main routine | |
wg.Done() | |
} | |
func workers() { | |
// Spin N number of worker threads | |
for i := 0; i < numThreads; i++ { | |
go func(w int) { | |
for p := range ch { | |
fmt.Printf("Worker %d: %s\n", w, p) | |
// Transform the data, wait for 1000 records | |
// and save it to postgres | |
// time.Sleep(20 * time.Millisecond) | |
wg.Done() | |
letter = p | |
} | |
fmt.Printf("No more work to do, shutting down worker %d\n", w) | |
}(i) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment