# README
Mutex experiment note
- We can just put sync.Mutex inside the struct to inherit Lock() and Unlock() method
- Tips in using mutex:
- Make sure we only lock a certain critical path, don't lock part of code that requires I/O operation
- We can defer unlock if in the code, there are many return. This it to prevent unlocked lock + make code more tidy
- We cannot use defer in for loop, will cause deadlock
- If we lock 2 times, then it will cause deadlock --> locking 2 times
- If we lock 1 times and no unlocking happens, the deadlock will not happen
- We can use RWMutex if the case is that we want to do read and write operation.
- Why can't we use the usual Mutex for this case? because in order to get the correct value when we read, we need to do locking first. In some cases, the read operation can require more than 1 times locking. Therefore we can use RLock()
- Benefit of using RWMutex is that using this RWMutex:
- We are guaranteed that the shared state will not be mutated when any RLock is still there --> No Lock (Write Lock) will happen when RLock is still acquired
- Multiple read action by goroutine can access the critical section at the same time --> higher throughput
# Functions
No description provided by the author
No description provided by the author