1

The relatively large size of std::mutex on different platforms (e.g. 40 or even 80 bytes) is often explained as ensuring the mutexes don't occupy the same cache-lines and so don't incur logically unnecessary fetches and synchronization activity.

However a std::mutex is commonly embedded in a larger object and its very size is (almost by design) a detriment to true sharing.

What is a good designs for a minimal mutex?

A one-byte spin-lock design is fairly trivial but a 'true' mutex in which threads suspend until notified is more of a challenge. I can't find a portable way for a thread to notify another thread to unsuspend/unsleep without resorting to a mutex.

Persixty
  • 355
  • 2
  • 10

1 Answers1

2

WTF::Lock, the locking library used by WebKit sounds like what you need. The link above describes the design in detail.

The most important points for you are:

  • The Mutex only uses one byte, which is enough for a spin-lock.
  • It switches from spin-locking to blocking for longer waits. This is implemented via thread queues which are separated from the mutex itself.

I'm not sure how usable the library is without WebKit, but the design principles described in the linked post are generally applicable. For example there is a popular Rust locking library with the same design.

CodesInChaos
  • 5,847