It seems like there are no cases when one can conclude that a memory_order_seq_cst operation A precedes another one B in their single total order and yet neither A happens before B, nor B happens before A.

Thus, because of that and N4700 [atomics.order] 32.3\ 3, I assert that relations "happens-before" and "precedes in a single total order" are equivalent. So all these rules: 32.3\ 3, 4, 5, 6, 7 are redundant. They are already covered by coherence rules in [intro.races] 6.8.2.1\ 14, 15, 16, 17 and fence rules in [atomics.fences] 32.9\ 2, 3, 4.

  • 1
    I don't know the answer to your question, but I'd bet 10:1 odds that there is a case you didn't consider. ;) – Yakk - Adam Nevraumont Nov 20 '17 at 14:37
  • @Yakk probably involving an operation C that isn't memory_order_seq_cst – Caleth Nov 20 '17 at 14:39
  • 1
    @Caleth Ah, the OP is only asking about sequences of memory_order_seq_cst, not how they interact with other (kinds of) operations? Yes, i can believe that, because memory_order_seq_cst is the strongest ordered memory ordering. Most of the text will be handling corner cases for the more relaxed memory orderings. – Yakk - Adam Nevraumont Nov 20 '17 at 14:40
  • @Yakk "The truth is out there" : o – Eugene Zavidovsky Nov 20 '17 at 14:55
  • 2
    It's not clear whether you claim these two relations are equivalent for seq_cst operations, for seq_cst + nonatomic operations, or for all operations. They are definitely not equivalent for memory_order_acquire operations, because acquire operations can participate in happens-before relations, but they can't participate in the single total order S mentioned in 32.3. If it helps, I think your claim might be true for the set of seq_cst + nonatomic operations taken together, assuming the program is data-race free, but I admit I haven't thought too hard about it. – preshing Nov 20 '17 at 16:18
up vote 1 down vote accepted

It seems like there are no cases...

Oh, well, there it is.

#include <iostream>
#include <thread>
#include <atomic>
#include <cassert>

std::atomic<bool> x = {false};
std::atomic<bool> y = {false};
std::atomic<int> z = {0};

void write_x()
{
    x.store(true, std::memory_order_seq_cst);
}

void write_y()
{
    y.store(true, std::memory_order_seq_cst);
}

void read_x_then_y()
{
    while (!x.load(std::memory_order_seq_cst))
        ;
    if (y.load(std::memory_order_seq_cst)) {
        ++z; 
    }
}

void read_y_then_x()
{
    while (!y.load(std::memory_order_seq_cst))
        ;
    if (x.load(std::memory_order_seq_cst)) {
        ++z;
    }
}

int main()
{
    std::thread a(write_x);
    std::thread b(write_y);
    std::thread c(read_x_then_y);
    std::thread d(read_y_then_x);
    a.join(); b.join(); c.join(); d.join();
    assert(z.load() != 0);  // will never happen
}

This example demonstrates a situation where sequential ordering is necessary. Any other ordering may trigger the assert because it would be possible for the threads c and d to observe changes to the atomics x and y in opposite order.

  • @Deduplicator Done... Damn it! That portion of the standard should not be so complicated. I have some thoughts about modification to the "happens-before" relation. I shall add them later, if they seem correct and interesting... – Eugene Zavidovsky Dec 13 '17 at 20:35
  • No, I don't know a clearer description than that in the standard. Unfortunately, it is indeed somewhat complicated... – Eugene Zavidovsky Dec 14 '17 at 11:52

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.