sync.Mutex notes for golang

sync.Mutex

Mutual exclusio (mutex) . The Mutex in the sync package is its corresponding type. The value of this type can be called a mutex or a mutex.

A mutex can be used to protect a critical section or a group of related critical sections. We can use it to ensure that only one goroutine is in the critical zone at the same time.

In order to honor this guarantee, whenever a goroutine wants to enter the critical section, it needs to be locked first, and each goroutine must unlock it in time when it leaves the critical section.

The lock operation can be achieved by calling the Lock method of the mutex, and the unlock operation can be called the Unlock method of the mutex.

 

The considerations for using mutex locks are as follows:

1. Do not lock the mutex repeatedly;

2. Don't forget to unlock the mutex, use defer statement if necessary;

3. Do not unlock the mutex that has not been locked or unlocked;

4. Don't pass the mutex directly between multiple functions.

 

Let each mutex lock only protect a critical section or a group of related critical sections.

Once we declare a variable of type sync.Mutex, we can use it directly. Note, however, that this type is a structure type and belongs to one of the value types. Passing it to a function, returning it from a function, assigning it to other variables, and letting it enter a certain channel all result in its copy.

And, the original value and its copy, and multiple copies are completely independent, they are completely independent, they are different mutex locks.

If you pass a mutex lock as a parameter value to a function, all operations on the incoming lock in this function will not have any effect on the original lock that exists outside the function.

 

 

sync.RWMutex

Read-write lock is short for read / write mutex lock . In Go, read-write locks are represented by values ​​of type sync.RWMutex . Like the sync.Mutex type, this type is also available out of the box

A read-write lock actually contains two locks, namely: read lock and write lock. The Lock method and Unlock method in sync.RWMutex type are used to lock and unlock the write lock, respectively, while its RLock method and RUnlock method are used to lock and unlock the read lock, respectively.

A read-write lock actually contains two locks, namely: read lock and write lock. The Lock method and Unlock method in sync.RWMutex type are used to lock and unlock the write lock, respectively, while its RLock method and RUnlock method are used to lock and unlock the read lock, respectively.

 

There are the following rules for the same read-write lock:

Attempting to lock the write lock while the write lock is locked will block the current goroutine.

Attempting to lock the read lock while the write lock is locked will also block the current goroutine.

Attempting to lock the write lock while the read lock is locked will also block the current goroutine.

Trying to lock the read lock while the read lock is locked will not block the current goroutine.

For a shared resource protected by a read-write lock, multiple write operations cannot be performed simultaneously, nor can write operations and read operations be performed simultaneously, but multiple read operations can be performed simultaneously.

 

 

 

 

Published 127 original articles · Likes 24 · Visits 130,000+

Guess you like

Origin blog.csdn.net/Linzhongyilisha/article/details/105470984