12 ms·
I maintain my own FOSS threads signals/events libraries in C++ [0] and in rust [1] (atop of parking lot as a futex shoe-in) and I disagree. This has nothing to
by ComputerGuru 4y ago
I maintain my own FOSS threads signals/events libraries in C++ [0] and in rust [1] (atop of parking lot as a futex shoe-in) and I disagree. This has nothing to do with the language and everything to do with the semantics of the locks. Writing concurrency primitives is HARD and the more functionality your API exposes, the more room there is for nuanced bugs in how everything interplays with everything else.
[0]: https://github.com/neosmart/pevents https://github.com/neosmart/pevents
[1]: https://github.com/neosmart/rsevents https://github.com/neosmart/rsevents
- galangalalgol 4y agoI'm a rust beginner, but it seems like a mutex or lock is shared mutable state, and abstracting this with unsafe code to implement events is just creating a loophole by which to call a function on another thread without having a mutable reference? I'm sure I'm missing something, or a lot of things. It just aeems like code I wouldn't writ in c++ even anymore. I typically don't do signalling between threads or have them share mutable state. If they do signal it is by read or write to a value small enough to be atomic.
- ComputerGuru 4y agoRust’s synchronization types are built on the semantics of shared objects, with the synchronization object owning the data access to which is channeled exclusively via the synchronization type. Events and signals aren’t intended to be used off you’re trying to share ownership of memory or data - you should use the std types in that case. They’re for signaling between threads or for building your own synchronization objects atop of. For example, an auto reset event can be used in lieu of a one-bit channel (sort of like Mpmc<()>) and manual reset events can be used to implement the same in broadcast mode, much more efficiently than the channel crates. A common usage is to block until an event is available or a shutdown has been requested - in lieu of manually polling an AtomicBool and aborting if it is true, making your code more responsive (no delay between polls) and more efficient (no repeated polling, no busy waits, etc). They can be used to signal state transitions or readiness, which doesn’t have anything to “own”, for example, the rsevents-extra crate [0] implements a countdown event (event becomes available when n outstanding tasks have finished in parallel threads) or a semaphore (restricting concurrency to a region of code, not limiting access to a variable or object). [0]: https://github.com/neosmart/rsevents-extra https://github.com/neosmart/rsevents-extra
- asveikau 4y agoIt's worth noting that condition variables are a finicky interface. Speaking personally, I would typically much rather work with something like a semaphore. I also feel like I've seen a lot of misuse of condition variables out there.
- ComputerGuru 4y agoCondition variables were the POSIX equivalent to Win32’s WaitForMultipleObjects (and IOCP apis like it). Now both are available on Windows (and my library makes WFMO available on *nix/Linux/macOS). Semaphores alone can’t mimic the same semantics (juggling between multiple, disparate locks/sync objects) without running into a possible race condition unless you have an API that can somehow wait on one of two or more semaphores to become available.
- asveikau 4y agoI totally disagree with your assessment. WaitForMultipleObjects enters the kernel every time and blocks on kernel objects. The nearest equivalent is select, poll, epoll, kqueue, etc. Newer innovations like eventfd start to introduce synchronization objects into the mix of what type of kernel object to block on. A Win32 mutex or semaphore is very heavy and typically avoided in a sane high performance application unless you need it to cross process boundaries. A lot of the time (most?) you will want a win32 critical section, which is similar to the modern futex based pthread mutex implementation in that it does not enter the kernel unless there is contention. A pthread condition variable is clumsy. You need to give it an extra mutex and loop etc. It doesn't have any of the simplicity of WaitForMultipleObjects, even with all the faults of that interface of which there are many.
- ComputerGuru 4y agoI was not talking about implementation details but semantics. I agree with what you are saying. My own implementations are user-mode unless sleeping.
- enjoy-your-stay 4y ago