Re:Thread Safety in C++ and Rust
by haberman on Sunday December 19, 2021 @12:02AM
(#29611489)
I agree that the writes are synchronized with the reads in my example. But the reads are totally unsynchronized with respect to other reads, in every sense. That is what thread-compatibility guarantees you: concurrent reads without synchronization. A thread-unsafe type doesn't allow even that much.
[ Parent
| on HN ]
Re:Thread Safety in C++ and Rust
by randomNumber7 on Sunday December 19, 2021 @07:14AM
(#29613323)
Isn't it always safe if you only read? I can't imagine an example where a race occurs when all threads are just reading.
"Read" here is shorthand for "call a const method". You can write const methods that are not safe to call concurrently: for example, they could use "mutable" or "const cast" to perform mutation, or they could access a non-const pointer to shared state. If a type had a const method like this, the type would no longer be considered thread-compatible.
Re:Thread Safety in C++ and Rust
by efokschaner on Sunday December 19, 2021 @01:32AM
(#29611929)
Thanks, that makes sense.