Skip to content

Commit

Permalink
Mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
aditya43 committed May 30, 2021
1 parent f3e3a2a commit 2108ca9
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,4 +253,23 @@ select {
case v := <- ch:
case ch <- v:
}
```
```
-----------
## Mutex:
- Mutex is used to guard access to shared resource.
- sync.Mutex provides exclusive access to shared resource.
- If the Goroutine is just reading from the memory and not writing to the memory then we can use `READ WRITE MUTEX`.
- `sync.RWMutex` allows multiple readers. Writers get exclusive lock.
-----------
## When to use Channels vs. When to use Mutex:
- Channels:
* They are made to implement communication between Goroutines.
* Passing copy of data.
* Distributing units of work.
* Communicating asynchronous results.
- Mutex:
* When we have data such as Caches, States, Registeries which are big to be sent over the channels and we want access to this data to be thread safe. This is where classic synchronization tool such as Mutex comes into the picture.

0 comments on commit 2108ca9

Please sign in to comment.