-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
29 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# MUTEXES | ||
## A Brief Introduction | ||
Mutex- short for Mutual Exclusion- is like a lock that helps in controlling access to shared resources, like data or files, in a way that only one thread (or task) can access it at a time. This prevents race conditions, where two tasks try to change the same data at the same time, causing errors or inconsistency in results. Let us understand this by taking a very common example- Imagine, several people in a library want to read the same book and in doing so, they might get in each other's way and also mess up the book. So, to avoid this, the librarian can simply put a "lock" on the book, so that only one person can access it at a time. And once this person is done with reading, another person can smoothly access the book afterwards. | ||
|
||
## Working of a MUTEX | ||
In programming, it is especially useful while dealing with multithreading. Following is the process explained briefly:\ | ||
While some thread(or task) is using a particular resource, then using mutex, the resource can be Locked and another thread trying to access the same resource will have to wait until it is Unlocked. | ||
|
||
## Basics of MUTEX in GoLang | ||
To use Mutex in go, we import a library called _"sync"_ and create a variable of _"sync.Mutex"_ type to create a mutex. Then, before using the resource (that's to be accessed by mutiple threads), we can lock the mutex by using _"Lock()"_ method and later on unlock it by using the _"Unlock()"_ method. | ||
|
||
## Other Mechanisms: Semaphores | ||
Another mechanism similar to a mutex is called a semaphore. While a mutex allows only one thread to access data at a time, a semaphore allows multiple threads to access data, but with a limit on how many threads can access it at once.\ | ||
A semaphore is like having a limited number of keys to a door. Let’s say there are 3 keys. Up to 3 threads can hold a key and access the door simultaneously, but if all keys are taken, any other thread that wants access must wait until a key is released.\ | ||
To use this mechanism, we just need to create a channel( say "sem" ) of some buffer size (say n). This limits access to the resource to n goroutines at once. When a goroutine tries to access the resource, it sends an empty struct (struct{}) into the sem channel. If there are already n items in the channel, the goroutine will block (wait), until a slot becomes available. After accessing the resource, the goroutine removes an item from the sem channel, freeing up a slot for another goroutine. | ||
|
||
## In context of the MergeFest Project- _Memoria_ | ||
A more advanced version of mutexes is used in the project Memoria in a file named _"rwmutex.go"_ (file path: /usr/local/go/src/sync/rwmutex.go). RWMutex simply stands for Reader/Writer Mutex, which is a more advanced type of mutex used to manage concurrent access to shared resources. Specifically, it allows multiple readers to access the resource simultaneously, but ensures exclusive access to a single writer when writing to the resource. This is helpful for cases where reads are frequent, and writes are less frequent, making the system more efficient.\ | ||
To understand the process better, let us divide the code in the file in two components:\ | ||
\ | ||
**1. RWMutex struct {}**\ | ||
It comprises: _w_(mutex) , _writerSem_ and _readerSem_ (semaphores for writers to wait for completing readers and vice versa), _readerCount_(number of pending readers) and _readerWait_(number of departing readers).\ | ||
\ | ||
**2. The Locks**\ | ||
It comprises: RLock and Unlock (used to acquire a reader lock and unlock), Writer Lock and Unlock, TryLock(attempts to acquire the write lock without blocking), and RLocker(returns a Locker interface that allows RLock and RUnlock operations to be used with a RWMutex as if it were a standard Locker interface).\ | ||
|
||
# Conclusion | ||
Mutexes and semaphores are crucial tools for managing concurrency in programming, especially when multiple threads or goroutines are involved.Both mechanisms are essential for avoiding race conditions, ensuring data consistency, and preventing issues like crashes or data corruption. Understanding them is key to writing safe and efficient multi-threaded or concurrent programs in languages like Go. | ||
|