r/learnprogramming • • Apr 05 '22

Name one programming/comp sci concept you never understood and if you understand it, try to explain it to them

Name a programming concept such as virtual functions, pointers, certain algorithms, etc that you always had a hard time understanding and why.

Someone else who reads yours and does understand it, try to explain it to them to help them learn

1.6k Upvotes

738 comments sorted by

View all comments

2

u/BenardoDiShaprio Apr 06 '22 edited Apr 06 '22

Memory consistency. People explain the theory, but I dont understand the actual implementation, as in, how would adding an atomic with acquire differ from release, at the hardware level.

1

u/[deleted] Apr 14 '22

If everybody uses the same CPU then there wouldn't need to be an atomic memory model, as there is only one sort of behaviour. For instance, on x86 Relaxed is exactly the same as Acquire because everything is strongly ordered at hardware level.

But there are so many different architectures with subtly different behaviours. The main reason why we have a formal memory model is to abstract over these hardware differences, such that the same program exhibits predictable and well-defined behaviours when compiled against and run on different hardware.

Another reason for a formal model is to give programmers and compilers a consensus on what optimisations (such as instruction reordering) the compiler is allowed to make.

The question "how would adding an atomic with acquire differ from release" cannot be precisely answered without specifying a CPU model. But it could mean that a releasing store (triggered with a special instruction) causes some sort of cache synchronisation.