r/cpp
•
u/steveklabnik1
•
Oct 24 '24
•
1
Why Safety Profiles Failed
https://www.circle-lang.org/draft-profiles.html
180
Upvotes
View all comments
Show parent comment
3
u/duneroadrunner Oct 25 '24
Oh, the multi-threading documentation is in the documentation of the associated library. The short answer is that the associated library provides an "exclusive writer object" transparent template wrapper that is essentially the equivalent of Rust's
RefCell<>, and basically anything shared between threads is explicitly or implicitly wrapped in that. The rest of the multi-threading safety mechanism is similar to Rust (with equivalents ofSendandSynctraits, etc.). So basically the solution is similar to Rust's, but with the extra step of imposing the aliasing restrictions that most Rust references get automatically.To expand a little bit, the "exclusive writer object" wrapper is actually a specific specialization of a more general "access controlled object" template wrapper. The "exclusive writer object" wrapper corresponds to the "multiple readers xor single writer" restriction that is pervasive in Rust. But, for example, you could could also choose the more flexible "multiple readers from any thread xor multiple readers and/or writers from a single thread" restriction that more reflects the natural C++ (lack of) restrictions. This is actually the default one used. Notably, this has the benefit of providing "natural" "upgrade lock" functionality. That is, if you have a read (const) reference to an object, you can, in the same thread, also acquire a write (non-const) reference to the object without relinquishing the original read reference. Of course only if no other thread is holding a reference to the object at the time. The benefit being that if you don't relinquish the original read reference, then you don't run the risk of some other thread acquiring a write reference to the object before your thread does.