9

Which major OS / platforms implement wait morphing?

This question came up when I noticed that there's no clearcut best practice about whether one should signal a condition variable with mutex locked or not. A typical recommendation is to signal while holding the lock unless profiling shows a substantial performance improvement overhead from unlocking (by removing an extra context switch).

IIUC, the only disadvantage of holding the lock while signalling is the extra two context switches; the advantages are the lower risk of a bug, and easier to achieve real-time predictability.

So, it seems that if wait morphing is implemented, the practice of holding the lock while signalling is strictly better.

Kevin Yin
  • 844
  • 1
  • 8
  • 26
max
  • 49,282
  • 56
  • 208
  • 355
  • There is uncertain evidence at https://news.ycombinator.com/item?id=11893756 that Linux implements it. The [pthreads spec](http://linux.die.net/man/3/pthread_cond_signal) supports wait morphing, and the link claims that Linux futexes, which are behind condition variables, also support it. – Kevin Yin Jul 17 '19 at 02:01

1 Answers1

5

It's not supported on Linux. Mark Mossberg investigated it here, and it still holds true as of glibc master today (June 9, 2022).

Kevin Yin
  • 844
  • 1
  • 8
  • 26
  • 1
    Mark says: " fun fact: There's a widely assumed optimization with glibc condition variables ("wait morphing"/requeue) that was silently removed in 2016. Few seem to be aware of this, including the creator of libc++. It's beyond my ability to dig into why and what the implications really are, " – max Jun 10 '22 at 06:25