by jesse__ · 3y ago · 0 comments
As an aside -- it would be interesting to see what the analog on Windows is. I know from experience the platform-provided mutex implementation on win32 is horrendously slow, and it would be nice to know if it's even possible on that platform to implement a 'real' mutex that doesn't suck. Anyone know of a reference implementation of mutexes on Windows?
[1]: https://learn.microsoft.com/en-us/windows/win32/api/synchapi...
[2]: https://github.com/ziglang/zig/blob/295b8ca467da36cd1066395e...
If you don't need cross-process sync, you should really be using a Critical Section instead. The kernel-level interface, which is largely undocumented, is I believe either an Event object or a Keyed Event on later versions.
There's some interesting related discussion on that here: https://microsoft.public.win32.programmer.kernel.narkive.com...
Anyone know of a reference implementation of mutexes on Windows?
Look in the WINE source.
(That said, I think I heard somewhere that windows mutexes are decent now?)
Sorry, but this sounds like FUD to me. First off, there is no single Win32 mutex implementation. The Win32 'Mutex' object is indeed heavy weight, but it is meant for inter-process synchronization. For thread synchronization you would rather use 'CriticalSection' or 'SRWLOCK'; they both avoid calling into the kernel in the uncontended case. SRWLOCK in particular is very fast.
https://crates.io/crates/parking_lot
IIRC the only platform specific part is the thread parking logic, so I'd assume it's pretty fast on Windows in the common case.