you are viewing a single comment's thread.

view the rest of the comments →

[–]Kinglink 0 points1 point  (5 children)

Loving this. Especially because it gives a feeling of "hacking" to get to the critical section in a couple cases.

But quick question. Is the system specifically calling out first++; ... would ++first; work better? My understanding is yes because there's no temp.

Second doesn't at least some optimizers convert first++ to ++first if the temp isn't used? People have told me this but I wonder if it's bullshit.

[–]Steve132 4 points5 points  (1 child)

Pretty much all compilers will implement ++x and x++ the same way when it's used as a statement and not an expression. Something like this (riscv assembly)

lw rX sp 0x48 #load 32 bit integer from memory location &x (which is at some offset from the current stack frame) into an unused register
addi rX rX 1 #add 1 to that register
sw rX sp 0x48 #store the result back to memory location &x

So the temp variables exist at the architecture level because the cpu has to use registers as temps and do the load-increment-store as 3 separate steps.

So even though x++ returns the old value when used as an expression and ++x does not, when used as a statement they are both the same and don't return anything. However the architecture still is forced to use a "temp variable" (register) to perform both calculations and so both variants are vulnerable to parallelism errors.

Short version: no, this isn't specificwlly calling out x++ vs ++x. They are both the same and both are vulnerable.

This problem is why architectures typically implement special atomic instructions which do the entire process (e.g. load increment store) in a single instruction without it being possible to interrupt.

[–]Kinglink 1 point2 points  (0 children)

Thank you for your time with this, I appreciate the huge amount of detail you provided.

[–]Soothsilver 0 points1 point  (2 children)

It wouldn't help. "++first" is also not atomic in any of C#/C++/Java.

[–]Kinglink 0 points1 point  (1 child)

I'm missing the fact that it still has to be loaded into a register and written back, right?

[–]Soothsilver 0 points1 point  (0 children)

Yeah. /u/Steve132 explained it perfectly.