1

All over StackOverflow and the net I see folks to distinguish mutexes and spinlocks as like mutex is a mutual exclusion lock providing acquire() and release() functions and if the lock is taken, then acquire() will allow a process to be preempted.

Nevertheless, A. Silberschatz in his Operating System Concepts says in the section 6.5:

... The simplest of these tools is the mutex lock. (In fact, the term mutex is short for mutual exclusion.) We use the mutex lock to protect critical sections and thus prevent race conditions. That is, a process must acquire the lock before entering a critical section; it releases the lock when it exits the critical section. The acquire() function acquires the lock, and the release() function releases the lock.

and then he describes a spinlock, though adding a bit later

The type of mutex lock we have been describing is also called a spinlock because the process “spins” while waiting for the lock to become available.

so as spinlock is just a type of mutex lock as opposed to sleepable locks allowing a process to be preempted. That is, spinlocks and sleepable locks are all mutexes: locks by means of acquire() and release() functions.

I see totally logical to define mutex locks in the way Silberschatz did (though a bit implicitly).

What approach would you agree with?

mtmrv
  • 55
  • 1
  • 6
  • Which approaches are you referring to? Your question is unclear. – spongebob Nov 18 '20 at 19:18
  • Approach to define mutex lock. Many resources refer to mutex locks and spin locks as different things. And it differs from the approach of the book I mentioned. – mtmrv Nov 18 '20 at 19:26

3 Answers3

1

The type of mutex lock we have been describing is also called a spinlock because the process “spins” while waiting for the lock to become available.

Maybe you're misreading the book (that is, "The type of mutex lock we have been describing" might not refer to the exact passage you think it does), or the book is outdated. Modern terminology is quite clear in what a mutex is, but spinlocks get a bit muddy.

  • A mutex is a concurrency primitive that allows one agent at a time access to its resource, while the others have to wait in the meantime until it the exclusive access is released. How they wait is not specified and irrelevant, their process might go to sleep, get written to disk, spin in a loop, or perhaps you are using cooperative concurrency (also known as "asynchronous programming") and passing control to the event loop as your 'waiting operation'.

  • A spinlock does not have a clear definition. It might be used to refer to:

    1. A synonym for mutex (this is in my opinion wrong, but it happens).
    2. A specific mutex implementation that always waits in a busy loop.
    3. Any sort of busy-waiting loop waiting for a resource. A semaphore, for example, might also get implemented using a 'spinlock'.

    I would consider any use of the word to refer to a (part of a) specific implementation of a concurrency primitive that waits in a busy loop to be correct, if a more general term is not appropriate. That is, use mutex (or whatever primitive you desire) unless you specifically want to talk about a busy-waiting concurrency primitive.

orlp
  • 112,504
  • 36
  • 218
  • 315
  • Hi, thank you for the great answer! The definition cleared it up. I also agree it should be `2. A specific mutex implementation that always waits in a busy loop.` – mtmrv Nov 18 '20 at 23:12
1

A spinlock is a lock/mutex whose implementation relies mainly on a spinning loop.

More advanced locks/mutexes may have spinning parts in their implementation, however those often last for no more than a few microseconds or so.

spongebob
  • 8,370
  • 15
  • 50
  • 83
1

The words that one author uses in one book or manual will not always have the same exact meaning in every book and every manual. The meanings of the words evolve over time, and it can happen fast when the words are names for new ideas.

Not every book was written at the same time. Not every author is the same age or had the same teachers. It's just something you'll have to get used to.

"Mutex" was a name for a new idea not so very long ago. In one book, it might mean nothing more than a thing that keeps two or more threads from entering the same critical section at the same time. In another book, it might refer to a specific type of object in a certain operating system or library that is used for that same purpose.

Solomon Slow
  • 25,130
  • 5
  • 37
  • 57