Fix lecture

This commit is contained in:
Fedor Korotkiy 2020-03-26 17:57:49 +03:00
parent 29089ced33
commit 22fc994458
5 changed files with 38 additions and 136 deletions

View file

@ -3,6 +3,11 @@ Concurrency with Shared Memory
Фёдор Короткий Фёдор Короткий
* Concurrency with Shared Memory
- Принципы синхронизации, такие же как в C++ или java.
- Различается набор инструментов в стандартной библиотеке.
* Happens Before * Happens Before
- Одно событие в программе может _произойти_раньше_ (_happens_before_) другого события. - Одно событие в программе может _произойти_раньше_ (_happens_before_) другого события.
@ -296,3 +301,36 @@ Concurrency with Shared Memory
.play synconce/map.go /type result/,/OMIT/ .play synconce/map.go /type result/,/OMIT/
* sync.Cond
type Once struct {
done, running bool
mu sync.Mutex
cond *sync.Cond
}
func (once *Once) Do(f func()) {
once.mu.Lock()
defer once.mu.Unlock()
if done {
return
}
if running {
once.cond.Wait() // releases and acquires mutex
return
}
running = true
once.mu.Unlock()
f()
once.mu.Lock()
done = true
once.cond.Broadcast()
}
* golang.org/x/sync
Пакеты в golang.org/x содержат код, который не вошёл в стандартную библиотеку.
- errgroup - `sync.WaitGroup` в встроенной обработкой ошибок
- semaphore - семафор
- singleflight - дедубликация вызовов

View file

@ -1,67 +0,0 @@
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
package memo_test
import (
"testing"
"gopl.io/ch9/memo1"
"gopl.io/ch9/memotest"
)
var httpGetBody = memotest.HTTPGetBody
func Test(t *testing.T) {
m := memo.New(httpGetBody)
memotest.Sequential(t, m)
}
// NOTE: not concurrency-safe! Test fails.
func TestConcurrent(t *testing.T) {
m := memo.New(httpGetBody)
memotest.Concurrent(t, m)
}
/*
//!+output
$ go test -v gopl.io/ch9/memo1
=== RUN Test
https://golang.org, 175.026418ms, 7537 bytes
https://godoc.org, 172.686825ms, 6878 bytes
https://play.golang.org, 115.762377ms, 5767 bytes
http://gopl.io, 749.887242ms, 2856 bytes
https://golang.org, 721ns, 7537 bytes
https://godoc.org, 152ns, 6878 bytes
https://play.golang.org, 205ns, 5767 bytes
http://gopl.io, 326ns, 2856 bytes
--- PASS: Test (1.21s)
PASS
ok gopl.io/ch9/memo1 1.257s
//!-output
*/
/*
//!+race
$ go test -run=TestConcurrent -race -v gopl.io/ch9/memo1
=== RUN TestConcurrent
...
WARNING: DATA RACE
Write by goroutine 36:
runtime.mapassign1()
~/go/src/runtime/hashmap.go:411 +0x0
gopl.io/ch9/memo1.(*Memo).Get()
~/gobook2/src/gopl.io/ch9/memo1/memo.go:32 +0x205
...
Previous write by goroutine 35:
runtime.mapassign1()
~/go/src/runtime/hashmap.go:411 +0x0
gopl.io/ch9/memo1.(*Memo).Get()
~/gobook2/src/gopl.io/ch9/memo1/memo.go:32 +0x205
...
Found 1 data race(s)
FAIL gopl.io/ch9/memo1 2.393s
//!-race
*/

View file

@ -1,23 +0,0 @@
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
package memo_test
import (
"testing"
"gopl.io/ch9/memo2"
"gopl.io/ch9/memotest"
)
var httpGetBody = memotest.HTTPGetBody
func Test(t *testing.T) {
m := memo.New(httpGetBody)
memotest.Sequential(t, m)
}
func TestConcurrent(t *testing.T) {
m := memo.New(httpGetBody)
memotest.Concurrent(t, m)
}

View file

@ -1,23 +0,0 @@
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
package memo_test
import (
"testing"
"gopl.io/ch9/memo3"
"gopl.io/ch9/memotest"
)
var httpGetBody = memotest.HTTPGetBody
func Test(t *testing.T) {
m := memo.New(httpGetBody)
memotest.Sequential(t, m)
}
func TestConcurrent(t *testing.T) {
m := memo.New(httpGetBody)
memotest.Concurrent(t, m)
}

View file

@ -1,23 +0,0 @@
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
package memo_test
import (
"testing"
"gopl.io/ch9/memo4"
"gopl.io/ch9/memotest"
)
var httpGetBody = memotest.HTTPGetBody
func Test(t *testing.T) {
m := memo.New(httpGetBody)
memotest.Sequential(t, m)
}
func TestConcurrent(t *testing.T) {
m := memo.New(httpGetBody)
memotest.Concurrent(t, m)
}