samedi 20 août 2016

Acquire/Release semantics reordering

Based on the C++11 acquire/release semantics, I have the following questions:

Acquire semantics defines: The compiler/CPU should not reorder the reads that happen after the Acquire to happen before the Acquire. 1. But can it reorder the writes that happen after the acquire statement to before the Acquire??

Release semantics defines: The compiler/CPU should not reorder the writes that happen before the Release to happen after the Release. 2. But can it reorder the reads that happen before the Release statement to after the Release??

Example:

atomic<int> i = 0, x = 0;
atomic<int> j = x;
i = 20;

  1. Will the compiler/CPU reorder (optimize) the above to the following:

    atomic i = 20, x = 0;

    atomic j = x;

  2. But if we use a acquire load, will the compiler/CPU avoid reordering the write to i (i=20) ??

    atomic i = 0, x = 0;

    atomic j = x.load(memory_order_acquire);

    i = 20;

Aucun commentaire:

Enregistrer un commentaire