|
|
Log in / Subscribe / Register

Beingessner: Rust's Unsafe Pointer Types Need An Overhaul

Beingessner: Rust's Unsafe Pointer Types Need An Overhaul

Posted Mar 22, 2022 18:27 UTC (Tue) by khim (subscriber, #9252)
In reply to: Beingessner: Rust's Unsafe Pointer Types Need An Overhaul by jlombera
Parent article: Beingessner: Rust's Unsafe Pointer Types Need An Overhaul

> Again, "volatile" is to ask the *compiler* not to optimize access to memory in the generated binary code.

And that's not enough. Not even on x86. You know that, right?

Intel 8086 included lock prefix from the very beginning! And you can not force compiler to use it with volatile. End of story.

Yes, with C89 you had no choice but to use assembler with some volatile sprinkled here and there. C11 offers atomics which provide much more concise and usable semantic.

Don't use volatile except in kernel, please. It's not needed and harmful.


to post comments

Beingessner: Rust's Unsafe Pointer Types Need An Overhaul

Posted Mar 22, 2022 20:13 UTC (Tue) by jlombera (guest, #155698) [Link] (1 responses)

Sorry, I think we just keep talking about different things. You keep bringing atomics, reordering and serialization issues to the discussion. I already conceded those might be required in addition (or instead of) to "volatile" depending on what you are trying to achieve (e.g. serialization), but those are not the use case of "volatile".

Beingessner: Rust's Unsafe Pointer Types Need An Overhaul

Posted Mar 23, 2022 0:20 UTC (Wed) by excors (subscriber, #95769) [Link]

Could you give specific examples of use cases with shared memory between threads where volatile is sufficient?

The main thing I can think of is e.g. performance counters, where one thread updates them and another thread periodically reads them, and where you don't care if it reads slightly stale values (but not worse than a few usecs) or reads each counter in an unpredictable order. In that case, you do need something like volatile (to ensure the first thread doesn't hold the counter in a register for many usecs) but you don't need any further synchronisation guarantees. You also need atomic reads/writes, which I don't think volatile guarantees, but in practice it's probably okay if it's an aligned word-sized value.

Probably you could also do a simple form of mailboxes, where the producer thread does "while (m != 0) {}; m = 42;" and the consumer thread does "while (m == 0) {}; do_work(m); m = 0;", where (I think? but not certain) there are hopefully enough implicit dependencies that it will always behave as expected on any CPU. (But that won't work if you want to share more than a single word, because the mailbox message won't be synchronised with any other memory access.)

Those seem very niche cases, though. And you can easily do them with C++/C11 atomics using memory_order_relaxed (which adds no synchronisation barriers but does guarantee atomicity, like a more well-behaved volatile). I'm not aware of any drawbacks of memory_order_relaxed over volatile, and the benefit is it can be combined with acquire/release accesses (to the same variable or to others) for cases where synchronisation is important (which is nearly all cases involving shared memory).


Copyright © 2026, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds