Skip to main content
SOFe1970 u/SOFe1970 avatar

SOFe1970

u/SOFe1970

Feed options
Hot
New
Top
View
Card
Compact

I think every language has colors, they just don't make it as explicit as Rust does. And the noexcept thing is still C++ only and not in Rust, so technically C++ has one more color.



Again, this is literally the example in the link. Has anyone commenting here read the example on the StackOVerflow link at all?


That is literally the case when there are only receivers or only senders. Check the example yourself.


The semaphore example in the OP is a real world bug I found in someone's code many years ago (ok, I know they should have used a WaitGroup or an atomic int instead, but that's a different issue...). The fact that someone fell for it probably implies it is not "imagining things" and has actually caused misunderstanding.

This is an example where TOCTOU isn't a problem. If `len(ch) == 0`, there are no more receivers, and there are no senders at all, so `len(ch) == 0` is an eventual state. It will NOT transition to another state, so TOU (after TOC) will always have identical state as TOC.

The problem I demonstrated here is that TOU actually turns out to be before TOC (in terms of code order) due to CPU reordering memory accesses. And this is exactly what a global memory barrier is useful for, to ensure that TOC happens before TOU.


I suppose the downvotes are mostly people who are unhappy how I described an ambiguity in the specification as a possible contradiction (which I didn't intend to imply).


Well, the example is literally on the stackoverflow answer. Of course it's a terrible use case that could be replaced with WaitGroup; I just ran into it when fixing someone else's broken code.


Reading a value in the past is totally fine if it is monotonic. `len(ch) == 0` is a state that is never supposed to change once it happens, since there are no senders.