Answer the question
In order to leave comments, you need to log in
Mutex RWMutex differences?
Hello, I just can’t understand what are the differences between Mutex and RWMutex. I understand that RW is read / write, but what are the real differences from the usual Mutex? For example, I noticed that if you call mutex.Lock() in a loop, the processor core is fully loaded, but if you call RWMutex.RLock(), this is not the case.
Answer the question
In order to leave comments, you need to log in
RWMutex is needed when we have an object that cannot be written in parallel, but can be read in parallel. For example, the standard type map.
Before writing to the object protected by the mutex, a .Lock() is done, and calls to .Lock() and .RLock() in other goroutines will wait until you release the mutex with .Unlock().
Before reading the protected object, .RLock() is done and only .Lock() calls in other goroutines are blocked, .RLock() calls pass quietly. When you release a mutex via .RUnlock(), pending .Lock() calls can take the mutex in turn.
This ensures that the object is read in parallel by several goroutines, which improves performance.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question