Synchronization primitives can be implemented using std::atomic<T>
, at least their userspace part. With C++20 std::atomic<T>::wait
, they can be based on atomic only.
The question is whether it is worth using pointer-sized types or smaller types.
C++20 std::semaphore
is an example where max value is passed as template parameter, so the choice can be made at compile time, not hard-coded.
My thoughts so far:
I think that on platform where futex
uses specific variable size, should just use that size. On Windows with flexible WaitOnAdress
there's a room to pick.
32-bit types on x64 lead to smaller encoding and the same efficiency, so probably are the way to go. Further reducing to 16 and 8 bit types is apparently not useful (16-bit types result in larger encoding than 32-bit types).
There's also a case of Windows on arm, for which I'm not sure.
For std::semaphore
, the ability to pick integer type based on max is side effect of specializing binary semaphore, rather than an intention.