3 ms·
Certainly it's interesting that Mutex<T> is a thing in Rust but the equivalent (a mutex in the form of a wrapper type) doesn't exist in C++. One rationale is th
by tialaramex 4y ago
Certainly it's interesting that Mutex<T> is a thing in Rust but the equivalent (a mutex in the form of a wrapper type) doesn't exist in C++. One rationale is that Mutex<T> in Rust is actually safe, whereas the C++ equivalent would be an attractive nuisance since it looks safe but would be easily abused and if any of the mutex's users abused it you're screwed.
- smallstepforman 4y agoTo use the articles example, there are typically multiple bank accounts you want to update atomically, so guarding one account with a mutex doesn’t help you prevent deadlocks. The lock needs both accounts. The Mutex<T> example just doesn’t work with interacting objects.
- tialaramex 4y agoIn C++ you'd want to still also offer std::mutex because C++ doesn't have Zero Size Types, so a C++ Mutex<T> equivalent would always need space to store something. Mutex<()> is the same size as a hypothetical "mutex only" type and so Rust has no reason to offer a separate type representing a mutex which doesn't protect anything in particular. In fact even without actually putting anything of substance in the mutex, you can get value from type system judo using this mechanism, which C++ doesn't appear to do either.
- gpderetta 4y ago> C++ doesn't have Zero Size Types [[no_unique_address]] since C++20. Before that there was the empty base class optimization.
- tialaramex 4y agoNeither the Empty Base Class nor [[no_unique_address]] give C++ Zero Size Types. The [[no_unique_address]] attribute is a way to achieve something empty base classes were useful for without the accompanying problems, so that's nice, but it's not ZSTs. Can you say whether you genuinely thought C++ had ZSTs? And if so, how you came to that conclusion ?
- gpderetta 4y agoI'm not saying that C++ has zero size types. I'm saying that no_unique_address and EBO are a way to store a stateless object without it occupying any space, which is all you need to implement a zero space overhead Mutext<T> for stateless types.
- tialaramex 4y agoI think the complexity to deliver an equivalent of Mutex<T> which also works via no_unique_address to deliver no-space-overhead for deliberately stateless types that would otherwise add 1 byte to the type size is probably a bit much to ask. Thanks for pointing me to Boost synchronized_example<T> showing that this does exist, at least as an experimental library feature.
- gpderetta 4y agoIt is not exactly rocket science: https://gcc.godbolt.org/z/6Kz53bs7x https://gcc.godbolt.org/z/6Kz53bs7x. Bonus it supports visiting multiple synchronized at the same time, deadlock free.
- tialaramex 4y agoHuh. I was expecting that providing access to the no_unique_address value despite it not having an address would be much trickier than that.
- gpderetta 4y ago> Mutex<T> is a thing in Rust but the equivalent (a mutex in the form of a wrapper type) doesn't exist in C++ This is the synchronized value pattern [1]. I'm pretty sure that my 3rd edition of "The C++ programming Language" by Bjarne had a description of it and it predates rust by at least a decade. [1] https://www.boost.org/doc/libs/1_80_0/doc/html/thread/sds.html https://www.boost.org/doc/libs/1_80_0/doc/html/thread/sds.ht...
- tialaramex 4y agoThe Fourth Edition does not appear to mention this pattern under that name, and indeed it gives as an example burying the mutex inside the type to be protected, which has the same downside (the resulting object is bigger†) but not the upside (Stroustrup's approach means we can still forget to take the lock) That Boost link says it is "experimental and subject to change in future versions" but I don't know whether Boost just says that about everything or whether this would particularly mark out this feature. † In Rust Mutex<T> is 8 bytes bigger than T, typically. In C++ std::mutex is often 40 bytes.
- gpderetta 4y agoI don't have the copy of the book with me, so I don't know how Stroustrup called it. It was part of a discussion of overloading operator->. The boost warning doesn't have much to it. Boost libraries don't even guarantee API stability across versions and boost.synchronized has been available for a few years with no changes. > In Rust Mutex<T> is 8 bytes bigger than T, typically. In C++ std::mutex is often 40 bytes. That's because on libstdc++ std::mutex embeds a pthread_mutex_t which is 40 bytes for ABI reasons. It is a bad early ABI decision that can't be changed unfortunately. std::mutex on MSVC is worse. std::shared_mutex is much smaller on MSVC, but even worse than std::mutex on libstdc++. A portable Mutex<T> of minimal size can be built on top of std::atomic::wait though.
- tialaramex 4y agoMaybe the standard should then take the opportunity to define such a thing, since it would be smaller and more useful than what they have today in practice.
- grogers 4y agoNot in the stdlib, but it exists elsewhere, such as folly::Synchronized. There are some gotchas but it's a LOT better than a separate mutex and data. The main gotcha is instead of for (auto foo : bar.wlock()) { baz(foo); } You need to do bar.withWLock([](auto &lockedBar) { for (auto foo : lockedBar) { baz(foo); } }); In rust, the lifetime checking prevents that. The other big gotcha is accidentally blocking while holding a lock. E g. Instead of auto g = bar.wlock(); baz(*g); co_await quux(); You should do { auto g = bar.wlock(); baz(*g); } co_await quux(); Or use withWLock. If you co_await with the lock held you can deadlock if the executor switches to a different coroutine that tries to acquire the same lock. If you actually need to hold the lock across the blocking call, you need coroutine-aware locks, which turns it into auto g = co_await bar.wlock(); baz(*g); co_await quux(); No idea if rust prevents this problem - I suspect not, but I haven't used async rust.
- tialaramex 4y agoIt is possible to fall asleep in Rust while holding a lock, but it's possible to statically detect this mistake/ infelicitious choice in the software and diagnose it. Clippy calls this await_holding_lock - unfortunately the current Clippy diagnosis sometimes gives false positives, so that needs improving. Tokio provides a Mutex like your final example that is intended for use in such async code that will hold locks while waiting because Tokio will know you are holding the lock. It is accordingly more expensive, and so should only be used if "Don't hold locks while asleep" was not a practical solution to your problem.