7 ms·
Thread Safety in C++ and Rust
- RcouF1uZ4gsC 5y agoNice overview. One other thing to add about thread comparability is that different objects must be able to be used concurrently safely. This is important with objects that may have hidden shared state - for example a copy on write string.
- haberman 5y agoYes that is a good point. I will see if I can fit this into the article without introducing too much digression.
- pornel 5y agoAnother difference is that in C/C++ thread-safety documentation usually talks about functions being thread-safe or not. OTOH in Rust, the thread-safety information is attached to data types, not functions. Even closure types like `Fn() + Sync + Send` are not about the function itself, but the data it is allowed to capture. So in a sense, you're allowed to call any function anywhere you want, but Rust will stop you from giving it data that it couldn't safely touch.
- haberman 5y agoWhich documentation do you mean? The Abseil docs I linked to (https://abseil.io/blog/20180531-regular-types#data-races-and-thread-safety-properties https://abseil.io/blog/20180531-regular-types#data-races-and...) are talking about thead-safety of types, not functions.
- gumby 5y ago> OTOH in Rust, the thread-safety information is attached to data types, not functions. Which as the article points out means you pay the overhead for the thread safety every time you use the object wether you need to or not. So it's a design tradeoff, and I can make the argument for either case. However in my opinion Rust made the wrong choice. They chose the "you don't have to think of it" case, which I think is wrong for a systems programming language. If I want to make a bunch of changes to an object atomically (or a bunch of bookkeeping to multiple objects) I still need a manual lock (mutex), and then the internal bookkeeping is deadweight. I also might use an object on a single thread for a while and then hand it off to another thread which then has ownership (so still requires no locking). Or even hand it off to a group of threads which then need to use it in a safe way, presumably by calling different functions. I don't mean to imply there is One True Way or that Rust Got It Wrong.* This is merely one choice of many when exploring the design/functionality space. * OK on mixed case identifiers they did :-)
- geofft 5y agoMost of this internal bookkeeping is at compile time, right? Your average object that contains a bunch of mutable fields will, even in Rust, be expressed using a mutex. For instance, here's a very simple race-free program that transfers $20 to Alice from Bob and $30 from Bob to Alice: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=c6f47a6102afaf31a90b21e28cd43df4 https://play.rust-lang.org/?version=stable&mode=debug&editio... It uses struct Bank { alice_balance: i32, bob_balance: i32, } and keeps the bank inside a Mutex (or more specifically inside an Arc<Mutex<Bank>> to manage ownership across threads). This is a pretty straightforward pattern based on the Mutex docs: https://doc.rust-lang.org/std/sync/struct.Mutex.html https://doc.rust-lang.org/std/sync/struct.Mutex.html In this case, the individual threads that perform the transfer are not applying any additional overhead to alice_balance or bob_balance. Those are regular old int32_t variables in a regular old struct, same as C/C++. The only thing that's being prevented is that the compiler won't let me write a version of this program that doesn't have the Mutex. There's a lot of analysis going on inside the compiler to confirm this, but once the compiler is happy, there's no runtime overhead compared to the equivalent C/C++ program. (So I don't think "you don't have to think of it" is really what's going on. You have to think of it. It's just that if you don't, your program won't compile.) Now, you can write a version of the program that uses thread-safe data types for the balances directly, maybe something like this: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=9088c70ebfcae584de87bf38caa97b4d https://play.rust-lang.org/?version=stable&mode=debug&editio... struct Bank { alice_balance: AtomicI32, bob_balance: AtomicI32, } And it is true that there is now extra overhead in this version of the program, and the "advantage" is you could get rid of the mutex and it would still compile. But why would you write it this way? You know that you want the accesses to be transactional. You know you're going to want a mutex (or something) around the Bank. Why add the atomic types? If Rust somehow used atomic integers by default (or something like a global interpreter lock) so you didn't have to think about it and code compiled whether or not you locked things properly, then yes, I'd agree. But it doesn't - it uses normal integers and things fail to compile.
- kouteiheika 5y ago> However in my opinion Rust made the wrong choice. They chose the "you don't have to think of it" case, which I think is wrong for a systems programming language. As someone who has programmed in C++ for over 10 years and in Rust for over 5 years I have to hard disagree on this one. (: Rust made precisely the right choice because by default in gets rid of all of the mental overhead of "is this actually safe to call/move this across multiple threads?" while giving you the tools to bypass it if you actually want to do it the C++ way, and then you have to think about it. (But only if you explicitly choose to!) > If I want to make a bunch of changes to an object atomically (or a bunch of bookkeeping to multiple objects) I still need a manual lock (mutex), and then the internal bookkeeping is deadweight. Then just... don't add any internal bookkeeping to your type? (: Or, depending on the kind of bookkeeping you use, you might not have to pay for it anyway. For example, Rust has atomic integer types which require you to access them in a safe way, however, they also have this method on them: pub fn get_mut(&mut self) -> &mut u32 ...which means that if you wrap that in an outer Mutex you will have an &mut to it, which means that even though the type is thread-safe by default you do not have to pay anything extra to use it if you decide to put it in an outer Mutex and do the synchronization yourself. The overhead of its "built-in" synchronization is then completely gone! Isn't this the best of both worlds? (: > I also might use an object on a single thread for a while and then hand it off to another thread which then has ownership (so still requires no locking). ...so you'll have a &mut reference on that single thread, in which case no locking is required. Am I missing something? > Or even hand it off to a group of threads which then need to use it in a safe way, presumably by calling different functions. Fair enough. But in that case you can just use an UnsafeCell and do it the C++ way if you really want to.
- apatheticonion 5y agoAs someone new to Rust I appreciate the way you have articulated this.
- ridiculous_fish 5y agoThis seems rather like a hole in the scheme. There really are thread-unsafe functions like `setlocale` or `setupterm`, and the corresponding Rust crates allow you to use these functions from arbitrary threads. It's easy to produce a SIGSEGV by just invoking `ncurses::initscr()` a bunch in separate threads, without writing any unsafe code.
- firethief 5y agoThat's only a problem if you use code that is unsound. Well-written crates do not incorrectly mark functions safe.
- pornel 5y agoThread-unsafe C functions are a problem for Rust indeed. For example, C `setenv()` is unfixably broken for multi-threaded programs, and C code invoking it can crash Rust programs. Currently it's an open question what Rust should do about that. However, for Rust functions there's no hole, because Rust doesn't allow thread-unsafe global state. All globals always have to be immutable or use some synchronization primitive. Therefore, a safe Rust version of the `setlocale()` function would be forced to use atomics or mutexes to set the locale without data races. It's still a bad API, and Rust can't fix that.
- masklinn 5y ago> OTOH in Rust, the thread-safety information is attached to data types, not functions. That’s not entirely true, in the sense that you can have non-threadsafe functions operating on thread-safe types e.g. Mutex::get_mut.
- couchand 5y agoThe author seems to have missed that operations on atomics in Rust don't need &mut references.
- haberman 5y agoNot exactly. I was just trying to show the most direct port possible of the C++. In C++, Increment() is a non-const method, so in Rust I made increment(&mut self). The point is to show that thread-safe types are expressed differently in C++ and Rust. If you try to do things the C++ way in Rust, it doesn't work.
- couchand 5y agoStrange choice of demonstration, then. The C++ example is thread-safe, so you don't demand any further synchronozation from the caller. The Rust example, by contrast, DOES demand additional (superfluous) sychronization by the caller. The article makes it seem like Rust type system is just throwing up blockers for no reason. If your API is satisfactory with an &mut reference, then you don't need an atomic inside.
- haberman 5y agoThe point of the exercise is to show that the C++ idiom doesn't make sense in Rust. It's certainly not trying to imply that Rust is being unreasonable for rejecting a direct port of a C++ idiom. I do think the ambiguity mentioned in the article -- that &mut really means &uniq -- is the core of the confusion. If &mut were called &uniq, I would have had no reason to consider &uniq a direct port of a non-const pointer. That's probably the most important thing I learned when researching and writing this article: that &mut really means "exclusive", not "mutable."
- couchand 5y agoBut the C++ example does indeed make sense in Rust, if implemented appropriately. Sometimes you need an atomic, and the type that contains one should require no further synchronization. In both languages it's an equally useful tool. I agree that more could be done to clarify that &mut should be read "exclusive". You have an opportunity to help in this article. Don't just leave the compiler error and move on, which gives the impression that modifying an atomic is just impossible in Rust... Take the chance to explain why the naive port is not idiomatic.
- fulafel 5y agoThis is very kid-gloves on C++. In C++ these are basically just aspirations without any actual safety assurances.
- kwenfjkwenf 5y agoYes, of course. C++ is not safe. That's the whole point of the language.
- stathibus 5y agoI wouldn't say that's the _point_
- kwenfjkwenf 5y agoI would argue it is. It gives powerful, raw, and undisturbed access to the internals of a computer, optionally wrapped in a high-level wrapped. The undisturbed power is the point, and necessarily comes with unsafety.
- IshKebab 5y agoHe's right. Lack of safety is clearly not the point of C++. Nobody said "and we want this language to be unsafe!". It's unsafe as an unfortunate side effect of the real point of C++: zero cost abstractions. If there had been a way to make C++ safe and have zero cost abstractions (and be backwards compatible with C!) I'm sure they'd have done it.
- fulafel 5y agoC compatibility is the showstopper. And since everything was built on unsafe foundations of C compatibility, there could be no coherent language design principles serving safety.
- jstimpfle 5y agoA few thoughts and questions by a layman interested in distributed and concurrent programming. I would be very interested to hear back from someone with deep experience in this area. In my limited perception we are moving further and further away from fine-granular synchronization, and many use cases can be solved efficiently with off-the-shelf synchronization methods like queues. Given that, is thread safety that much of an issue anyway? What are folks' use cases where thread correctness is hard to get right? Where are more complex concurrent data structures needed - like, say, a concurrent hashmap? I gather that in kernels like Linux for example, there is a lot of this because it is attempted to serve as many syscalls as possible in the calling thread. My idea is that complexities like this come partly due to the fact that a running OS is a loose collection of software that wasn't developed by a single entity, so necessarily there was a lack of opportunity to measure requirements and allocating appropriate storage and computing resources ahead of time. In systems where the requirements are clearer cut, maybe appropriate resources can be more easily allocated upfront to achieve simplicity and possibly higher throughput at the cost of a little added latency (communication). It also seems like the fine granular model could become more obsolete over time as shared state becomes costlier with growing core count and the higher synchronization cost that I assume must come with it.
- mrfox321 5y agoDepends on what you are working on? Cross-request cache's naturally require synchronization. Although you could simply get away with thread-local storage. In huge systems, there's always some other thread doing stuff that you may be using in a way that hides the fact that it is thread-safe. I'm no expert, mind you. I avoid concurrency problems if possible, but they are sometimes unavoidable. I touch shared resources that would OOM if made thread-local.
- Animats 5y agoIn my limited perception we are moving further and further away from fine-granular synchronization, and many use cases can be solved efficiently with off-the-shelf synchronization methods like queues. Given that, is thread safety that much of an issue anyway? What are folks' use cases where thread correctness is hard to get right? Where are more complex concurrent data structures needed - like, say, a concurrent hashmap? I'm working on a client for a virtual world. This has a whole 3D world, or at least the part you can currently see, in memory. Anything can change, but most of it is static. There are quite a few threads working in coordination to keep the world updated. Some of this is painful in Rust, because Rust really wants an ownership tree. It can be made to work with reference counts (rust "Rc" and "Arc") and their weak forms as backlinks. It's kind of clunky. Single owner with backlinks, along with the static analysis needed to guarantee safety, is needed. That's a hard problem. Much of the "use a queue" thinking comes from the web back end crowd, where you're managing a large number of connections which have very little interaction with each other. Most of the state is in the database, anyway. It also seems like the fine granular model could become more obsolete over time as shared state becomes costlier with growing core count and the higher synchronization cost that I assume must come with it. Now that is a very real problem. The next frontier in language design is support for non-shared-memory systems all working on the same problem. I knew someone who spent too much time on that with the Playstation 3's Cell processors. Sony went back to more traditional architectures after it became clear how hard it was to do anything that way. The supercomputer people have been dealing with this for years, but mostly they want to handle big matrices, where the inner loop is tiny. Same with GPUs - shaders are highly parallel but small programs.
- tptacek 5y agoThis is a pretty great explanation that links to a rabbit hole of further intuition tuneups around &mut. I come to Rust's way of looking at the world via C/C++, and after following the links here it turned out I had a bunch of fucky intuitions about what Rust is trying to express. They really should have renamed `mut`! Having said that: I'm proof that you can get a bunch of stuff done in Rust without having a perfect mental model of this stuff. :)
- saghm 5y agoIt's common to hear people refer to &T and &mut T not as "mutable" or "immutable" but "shared" versus "unique". What probably muddles this is that the `mut` keyword _also_ is used for declaring things mutable or immutable, just at the binding level rather than the type level. These are related due to the fact that you can't take a `&mut` reference to a binding that isn't declared as `mut`, which probably also muddles the water a bit.
- tptacek 5y ago&mut is kind of gross syntactically anyways; another thing that would have clarified this would have been to rename & to &shared, which has symmetry.
- ReactiveJelly 5y agoNah, I like having immutable syntax be shorter. It makes idiomatic code shorter than code that does a lot of mutating
- saghm 5y agoAgreed! I also like that declaring a mutable binding is extra code beyond a regular binding rather than something the same (or shorter). At least for me, making things immutable by default and then only relaxing it when necessary leads to code that tends to be less buggy, and I suspect this might be true for others as well.
- 5y ago
- efokschaner 5y agoThis is the first time I've heard of "thread compatible" objects/state. The author states: "Most types in C++ are thread-compatible, as this guarantee comes mostly comes for free", and that "Any concurrent call to a non-const method must be synchronized by the caller." My understanding is that due to things like memory tearing, or the possibility of invariants between object members being in intermediate states, reads are always unsafe when there is a possibility of writes. Therefore, you can't selectively skip having some kind of synchronisation for the const calls (the reads), as something needs to be synchronizing all the reads against any potential writes. So to me this just reads as, "the caller has to manage all the thread safety for all the accesses", which I'd agree is what typically "comes for free" in c++, but I'd hardly call it a "guarantee". I'm wondering if I'm not seeing some meaningful level of "thread compatibility" in between "thread-safe" and the "accessor beware" behaviour of all c++ that wasn't designed to be thread-safe. Or is there an example of some c++ that is not even "thread-compatible" once the caller has accepted the burden of synchronization?
- ReactiveJelly 5y ago> My understanding is that due to things like memory tearing, or the possibility of invariants between object members being in intermediate states, reads are always unsafe when there is a possibility of writes. Correct. Rust calls this "aliasing XOR mutability". You can either have any number of readers reading the data as constant, or you can have one writer mutating the data. You can never have reads and writes at the same time. (unless you use, e.g., a mutex, which allows writes that _look like_ reads from the type system's PoV, which is safe)
- nyanpasu64 5y agoI interpreted the author saying "thread-compatible" to mean "accessor beware". Surprisingly in some cases, in C++ it's unsafe for multiple threads to even call const member functions on the same object, for example when the const method modifies a `mutable` field. One way this could happen is when a "const" method lazily computes a value and stores it in a field.
- haberman 5y ago> reads are always unsafe when there is a possibility of writes. That is correct for any reads that could race against a write. Writes must be synchronized with all other operations. But reads may proceed in parallel with other reads without synchronization. For example, if you have an object that is written in the thread where it is constructed, and only released to other threads once the mutation is complete, then you do not need to synchronize readers. Because the nature of the data flow ensures that none of the writes can race with the reads. > So to me this just reads as, "the caller has to manage all the thread safety for all the accesses" Not all accesses. Just the ones where a writer may race with another write or a read. > Or is there an example of some c++ that is not even "thread-compatible" once the caller has accepted the burden of synchronization? Thread-unsafe types do not even allow two reads to be unsynchronized.
- Orou 5y agoI was just reading an article earlier today on the same subject. I thought the "pedagogy" section was interesting when considering if `mut` is the right term to be using for exclusive references. https://docs.rs/dtolnay/0.0.9/dtolnay/macro._02__reference_types.html https://docs.rs/dtolnay/0.0.9/dtolnay/macro._02__reference_t...
- exdsq 5y agoI read all these posts on Rust and it makes me wonder if I'm being stupid spending time on C++? I want a language that I can focus on for the foreseeable part of my career. I'm looking for stability and am interested in domains like finance and robotics. My key wants are that I can deep dive a language for as long as possible, as I'm burning out from constantly changing languages after ~8 years. I need some sort of external validation before I jump on five different things and fail lol.
- rapsey 5y agoRust job market is 1% of the C++ job market. Most programmers never heard of HN. Most jobs don't advertise here.
- otabdeveloper4 5y agoThe actual stuff is being written in C++ still. Rust is for "programming is hard, let's go (language) shopping" programmers. Unsurprisingly, they don't tend to finish their projects.
- IsThisYou 5y agoReal programmers use butterflies.
- tomjen3 5y agoEven if there is no new C++ project ever, there is tons and tons of legacy products that needs to be maintained (they are not all going to be rewritten, just as the old cobol projects aren't) and C++ is a deep language that really could benefit from extreme knowledge (a const rvalue reference? wtf). Rust may fail, but it is probably worth learning some of the ideas - maybe you could use those ideas in your C++ projects?
- exdsq 5y agoOh I've spent years working with Haskell and work with Rust right now! And I absolutely love diving into the pros of awesome type systems. I've actually worked with Golang, F#, Scala, Typescript, Java, C#, C++, Python, and probably a few others (all professionally, as in I've shipped something to prod) over the last few years. I used to love this but now I'm getting burned out with it all and want to learn a language in more depth.
- sakex 5y agoIt's missing channels which are used more on more for thread safety
- sudarshnachakra 5y agoAgree with the conclusion of the article that both C++ (mutable/const_cast) & Rust (interior mutability) to hand-wave certain properties they vouch for to enable performance/flexibility. I guess Rust is better behaved in this regard to ensure that they do not violate invariants in case of types like Cell/RefCell/Mutex, but indeed the design of the language itself caused all these problems for Rust. Because the raison d'etre for Mutex is to share something between threads and calling a lock on it must mutate it, but the language does not allow mutable instances to be shared. Essentially Mutex is a paradox in Rust, which cannot be implemented but for unsafe.