|
4 | 4 | * Mutex(Read/write lock sync.rwmutex based on Mutex) |
5 | 5 | * Cond |
6 | 6 |
|
| 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 | + |
0 commit comments