On Sat, 18 Mar 2017 22:44:26 -0400
Ramine <
to...@toto.net> wrote:
> I have just read this about the C++11 Memory Model:
>
> Some programming languages offer SC in multiprocessor environment. In
> C++11, you can declare all shared variables as C++11 atomic types
> with default memory ordering constraints. In Java, you can mark all
> shared variables as volatile [1] [2].
>
> Because the C++11 atomic types guarantee sequential consistency, the
> outcome r1 = r2 = 0 is impossible. To achieve this, the compiler
> outputs additional instructions behind the scenes – typically memory
> fences and/or RMW operations. Those additional instructions may make
> the implementation less efficient compared to one where the
> programmer has dealt with memory ordering directly.
>
> Read here:
>
>
https://people.cs.pitt.edu/~xianeizhang/notes/cpp11_mem.html
>
> So, there is still a problem with C++, because so it is still error
> prone if you forget C++11 atomic types to ensure sequential
> consistency and it is less efficient with atomic types.
That's wrong, in C++ atomics are not less efficient. To achieve full
sequential consistency any language would use the same processor
instructions to achieve that (usually, an MFENCE barrier on x86/64).
And if you forget to use the correct types in any language, or fail to
synchronize correctly, your program will fail.
> So i still prefer Delphi and FreePascal that don't reorder loads and
> stores.
You are very naive. Of course the Delphi compiler reorders loads and
stores, or it couldn't optimize, and it may indeed eliminate some of
them entirely; and even if the Delphi compiler does not, the hardware
might. What you probably mean is that the Delphi compiler does not
reorder loads and stores when optimizing so as to violate its execution
ordering guarantees. Neither does C++ with respect to its memory model,
including its default of sequential consistency. The C++ memory model
forbids compilers optimizing in a way which violates it. There are all
sorts of optimizations a compiler can carry out on single threaded code
that it cannot carry out on multi-threaded code.
I say "probably" above because I do not think you understand how to
program properly in Delphi either. You need to synchronize Delphi code
when using multiple threads, just as you need to synchronize C++ code
when using multiple threads.