-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
66 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
File renamed without changes.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Mutex fairness | ||
```go | ||
// Mutex fairness | ||
// | ||
// Mutex can be in 2 modes of operations: normal and starvation. | ||
// In normal mode waiters are queued in FIFO order, but a woken up waiter | ||
// does not own the mutex and competes with new arriving goroutines over | ||
// the ownership. New arriving goroutines have an advantage -- they are | ||
// already running on CPU and there can be lots of them, so a woken up | ||
// waiter has good chances of losing. In such case it is queued at front | ||
// of the wait queue. If a waiter fails to acquire the mutex for more than 1ms, | ||
// it switches mutex to the starvation mode. | ||
// | ||
// In starvation mode ownership of the mutex is directly handed off from | ||
// the unlocking goroutine to the waiter at the front of the queue. | ||
// New arriving goroutines don't try to acquire the mutex even if it appears | ||
// to be unlocked, and don't try to spin. Instead they queue themselves at | ||
// the tail of the wait queue. | ||
// | ||
// If a waiter receives ownership of the mutex and sees that either | ||
// (1) it is the last waiter in the queue, or (2) it waited for less than 1 ms, | ||
// it switches mutex back to normal operation mode. | ||
// | ||
// Normal mode has considerably better performance as a goroutine can acquire | ||
// a mutex several times in a row even if there are blocked waiters. | ||
// Starvation mode is important to prevent pathological cases of tail latency. | ||
``` | ||
|
||
## 基本流程 | ||
互斥锁的本质就是: 一堆人同时调用`Lock`方法, 只有一个人能顺利return,继续执行代码,也就是所谓的"拿到锁"。其他人会被阻塞在`Lock`方法里,直到有别人将它们唤醒。 | ||
|
||
在了解了大概的流程之后,我们再来看看它具体的实现: | ||
TODO: 互斥锁很推荐《Go 并发编程实战课》的相关章节 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
sync.Map | ||
=== |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
sync.WaitGroup | ||
=== | ||
## 使用 | ||
WaitGroup的注释已经很好的描述了它的用法: | ||
|
||
> `WaitGroup`用来等待goroutine集合完成。主goroutine调用`Add`来设置等待的数量。然后每一个goroutine在运行完成时调用`Done`。`Wait`在所有goroutine都完成之前会一直阻塞。 | ||
## 内部实现 | ||
WaitGroup的实现比较简单,整个`waitgroup.go`带上注释也只有100多行。 | ||
WaitGroup结构体的定义如下: | ||
```go | ||
type WaitGroup struct { | ||
noCopy noCopy | ||
|
||
state atomic.Uint64 // high 32 bits are counter, low 32 bits are waiter count. | ||
sema uint32 | ||
} | ||
``` |
File renamed without changes.
File renamed without changes
This file contains 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
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes