0

From The Art of Multiprocessor Programming by Herlihy:

If a thread cannot immediately acquire a lock, it can either spin, repeatedly testing whether the desired event has happened, or it can block, giving up the processor for a while to allow another thread to run.

Chapter 7 Spin Locks and Contention

... In this chapter, we turn our attention to locks that use spinning.

Chapter 8 Monitors and Blocking Synchronization

8.1 Introduction 177

8.2 Monitor Locks and Conditions 178

8.2.1 Conditions 179

8.2.2 The Lost-Wakeup Problem 181

8.3 Readers–Writers Locks 183

8.3.1 Simple Readers–Writers Lock 184

8.3.2 Fair Readers–Writers Lock 185

8.4 Our Own Reentrant Lock 187

8.5 Semaphores 189

Since Chapter 7 is for spin locks, and the title of Chapter 8 has "blocking", is Chapter 8 all for locks that "block" instead of "spinning"?

In particular, are Readers-Writers locks and Reentrant Locks locks that "block" instead of "spinning"?

Thanks.

Community
  • 1
  • 1
  • Did you read [the documentation](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html#lock--)? – chrylis -cautiouslyoptimistic- Oct 09 '17 at 01:25
  • Thanks. Do you mean any implementation of the interface `Lock` in Java must be a lock that blocks instead of spinning? –  Oct 09 '17 at 01:27
  • If it conforms to the interface, yes. (And outside of kernel use, I don't think any skilled programmer uses busy-waits. If you see an API with blocking and non-blocking lock acquisition methods, you can reasonably assume that the blocking ones will park or deschedule the thread.) – chrylis -cautiouslyoptimistic- Oct 09 '17 at 01:37
  • Thanks. Why can I "assume that the blocking ones will park or deschedule the thread"? What do you mean by "park or deschedule the thread"? –  Oct 09 '17 at 01:46
  • I think the jdk locks will spin very briefly, and then park. Can't find a reference for that right now, though, because I'm on my phone. – yshavit Oct 09 '17 at 01:51
  • "Parking" and "descheduling" basically means that the threads get taken out of the scheduling table, to be woken up at some future time. If an API is advanced enough to allow you to select from blocking or non-blocking acquisition, you can generally trust that the implementers are competent enough to handle the blocking case efficiently. – chrylis -cautiouslyoptimistic- Oct 09 '17 at 02:16
  • @chrylis Thanks. In general concept (for OS, libraries, languages concepts, not specific one such as Java), are readers-writers locks and reentrant locks necessarily blocking or spinning, or may be either? –  Oct 11 '17 at 00:48
  • You will almost never see a built-in spinlock. In the extremely unlikely case that the client wants the behavior, it could always try within a while loop. – chrylis -cautiouslyoptimistic- Oct 11 '17 at 00:57
  • [The JVM may use spin-locks to implement `synchronized`](https://stackoverflow.com/questions/20689718/what-is-adaptive-spinning-w-r-t-lock-acquisition). – Raedwald Dec 20 '18 at 15:17

0 Answers0