r/cpp • • Oct 24 '24 • Take My Silver 1

Why Safety Profiles Failed

https://www.circle-lang.org/draft-profiles.html
180 Upvotes

347 comments sorted by

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 of Send and Sync traits, 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.

3

u/duneroadrunner Oct 25 '24

Hmm, that answer might have been a bit wandering. I think the direct answer you're looking for is that run-time checks are used to prevent the aliasing that could otherwise cause data race issues.

But if it's performance you're concerned about, I don't think that's really an issue in the case of multi-threading. I think the small cost of such (unsynchronized) run-time checks would presumably be dwarfed by the synchronization cost involved in communicating with (or launching) the other thread. I'd be interested in any measurements otherwise.

Memory safety approaches that do and don't (universally) prohibit mutable aliasing incur costs in different places. In practice, with full optimizations turned on, I assume that the overall average performance would be similar between the two. But in theory, with optimizations turned off, I would think the ones that don't prohibit mutable aliasing would have an overall performance advantage due to the fact that their run-time costs have a higher tendency to occur outside of inner loops.

3

u/ts826848 Oct 25 '24

No, you got your answer right the first time :P

I appreciate the additional info though. The costs of different approaches is pretty interesting to analyze to me and seems to be rather tricky to quantify well, especially if you consider "soft" differences caused by stuff like architectural differences as you say.

I'd love to try to write some comparisons myself but time is in short supply, as always :(

1

u/ts826848 Oct 25 '24

Oh, didn't realize it was in the library documentation. Seems like I have some fun to add to my reading list!

So in summary, it sounds like you have something like a reader-writer lock for the threading-related aliasing checks plus the Send/Sync equivalents? And the aforementioned prohibition of lifetime-relevant mutable aliasing handles the other bits of what Rust's mutable XOR aliasing restriction does?

1

u/duneroadrunner Oct 26 '24

Yeah, I kind of over-simplified it, but that's the gist. Multi-threading is one of the "less elegant" parts of the solution, in part due to C++ pointer/references not being naturally amenable to safe asynchronous sharing. And it probably doesn't help that the documentation isn't the greatest at the moment, but there are usage examples for each item. The documentation does kind of assume the reader is familiar with (traditional) C++ multi-threading and mutexes. And if you haven't already read that section, the term "scope pointer", by default, just means "raw pointer". If you have any questions, feel free to post them in the "discussion" section of the github repository, and any other feedback is welcome :)

1

u/ts826848 Oct 26 '24

I'll keep that in mind when I find time to sit down and devote some proper attention to the docs (hopefully sooner rather than later!). Thanks for taking the time to explain!