Skip to content

Commit 1365b3d

Browse files
committed
update
1 parent 13b2b50 commit 1365b3d

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

EN/sync-mechanism/sync.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,60 @@
44
* Mutex(Read/write lock sync.rwmutex based on Mutex)
55
* Cond
66

7+
8+
# Sync.WRMutex handles common read and write thread safety issues
9+
10+
* RWMutex (read-write lock)
11+
RWMutex is a single write multiple read lock that can be added to multiple read locks or one write lock
12+
Read locks block writes when occupied, not reads, and multiple goroutines can acquire read locks at the same time
13+
A write lock prevents other goroutines (read and write) from entering, and the lock is exclusive to that goroutine
14+
It is suitable for situations of reading more and writing less
15+
* The Lock() and Unlock()
16+
Lock() with a write Lock, Unlock() to Unlock the write Lock
17+
If there are other read and write locks before a write Lock is added, the Lock() blocks until the Lock is available, and to ensure that the Lock is available, the blocked Lock() call excludes the new reader from the acquired Lock, that is, the write Lock has more privilege than the read Lock, and the write Lock takes precedence when there is a write Lock
18+
Using Unlock() before Lock() causes panic exceptions
19+
* RLock() and RUnlock()
20+
RLock() reads the lock, RUnlock() reads the lock
21+
When RLock() adds a read lock, if there is a write lock, the read lock cannot be added. Read locks can be added when there are only read locks or no locks, and read locks can be loaded multiple times
22+
RUnlock() unlocks the lock, RUnlock() undoes the call to the word RLock(), and has no effect on other simultaneous read locks
23+
Calling RUnlock() without a read lock causes a panic error
24+
The number of RUnlock() must not be more than RLock(), otherwise panic will result
25+
26+
```
27+
var mutex *sync.RWMutex
28+
mutex = new(sync.RWMutex)
29+
mutex.Lock()
30+
//--code
31+
mutex.Unlock()
32+
```
33+
34+
# sync.WaitGroup usage
35+
36+
和使用管道场景以及的区别:
37+
WaitGroup 对象内部有一个计数器,最初从0开始,它有三个方法:Add(), Done(), Wait() 用来控制计数器的数量。Add(n) 把计数器设置为n ,Done() 每次把计数器-1 ,wait() 会阻塞代码的运行,直到计数器地值减为0。
38+
39+
**相对于使用管道来说,WaitGroup 更轻量级**
40+
And the difference between using pipeline scenarios and:
41+
Inside the WaitGroup object is a counter that initially starts at 0 and has three methods: Add(), Done(), and Wait() to control the number of counters. Add(n) sets the counter to n, Done() sets the counter to -1 each time, and wait() blocks the code until the counter is reduced to 0.
42+
43+
**WaitGroup is more lightweight than using pipes**
44+
45+
46+
```
47+
func main() {
48+
wg := sync.WaitGroup{}
49+
wg.Add(100)
50+
for i := 0; i < 100; i++ {
51+
go func(i int) {
52+
fmt.Println(i)
53+
wg.Done()
54+
}(i)
55+
}
56+
wg.Wait()
57+
}
58+
```
59+
60+
# sync.WaitGroup considerations
61+
* the counter cannot be negative
62+
* the WaitGroup object is not a reference type (**pass-through requires an address, otherwise it will cause a deadlock**)
63+

zh_CN/sync-mechanism/sync.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,46 @@
44
* Mutex (基于Mutex的读写锁 sync.RWMutex)
55
* Cond
66

7+
8+
# Mutex(互斥锁)
9+
Mutex 为互斥锁,Lock() 加锁,Unlock() 解锁
10+
在一个 goroutine 获得 Mutex 后,其他 goroutine 只能等到这个 goroutine 释放该 Mutex
11+
使用 Lock() 加锁后,不能再继续对其加锁,直到利用 Unlock() 解锁后才能再加锁
12+
在 Lock() 之前使用 Unlock() 会导致 panic 异常
13+
已经锁定的 Mutex 并不与特定的 goroutine 相关联,这样可以利用一个 goroutine 对其加锁,再利用其他 goroutine 对其解锁
14+
在同一个 goroutine 中的 Mutex 解锁之前再次进行加锁,会导致死锁
15+
适用于读写不确定,并且只有一个读或者写的场景
16+
17+
18+
```
19+
var mutex *sync.RWMutex
20+
mutex = new(sync.RWMutex)
21+
mutex.Lock()
22+
//--code
23+
mutex.Unlock()
24+
```
25+
26+
# sync.WaitGroup的用法
27+
28+
和使用管道场景以及的区别:
29+
WaitGroup 对象内部有一个计数器,最初从0开始,它有三个方法:Add(), Done(), Wait() 用来控制计数器的数量。Add(n) 把计数器设置为n ,Done() 每次把计数器-1 ,wait() 会阻塞代码的运行,直到计数器地值减为0。
30+
31+
**相对于使用管道来说,WaitGroup 更轻量级**
32+
33+
```
34+
func main() {
35+
wg := sync.WaitGroup{}
36+
wg.Add(100)
37+
for i := 0; i < 100; i++ {
38+
go func(i int) {
39+
fmt.Println(i)
40+
wg.Done()
41+
}(i)
42+
}
43+
wg.Wait()
44+
}
45+
```
46+
47+
# sync.WaitGroup注意事项
48+
* 计数器不能为负值
49+
* WaitGroup对象不是一个引用类型(**传参需要地址,否则会导致死锁**

0 commit comments

Comments
 (0)