3 ms·
Mutex and Spinlocks are abstractions / programming language constructs used to solve the following class of problems: Problem 1. We have a process with multip
by stuxnet79 7y ago
Mutex and Spinlocks are abstractions / programming language constructs used to solve the following class of problems:
Problem
1. We have a process with multiple threads of execution (e.g. A and B).
2. A and B will at some point act on the same data or object state.
3. Both A and B can't operate on the same data at the same time.
Mutexes function more or less as locks. They lock up your data to prevent unwanted race conditions. In multithreaded environments, a thread of execution has to acquire a mutex prior to accessing the data. Once it's done operating on the data, it releases the mutex so that another thread of execution can acquire it.
Mutexes are again, simply locks. But not all locks are the same. Locks come in different shapes and sizes and work in different ways. Same with mutexes, they can have different semantics.
Spinlocks are a kind of mutex that cause threads trying to acquire them to spin or loop around while waiting for the resource to be available.