3

Optimizing compiler can rearrange memory access and CPU can execute instructions out of order.

The question is: will separating statements with comma operator guarantee exact order of execution? Or the only way is using memory barriers (which are tricky and non-standart)?

If it won't, than what exactly is guaranted about order of execution of comma separated statements?

Amomum
  • 6,217
  • 8
  • 34
  • 62
  • 3
    Nothing. The comma operator is no different from simply two statements separated by `;` in this respect. Remember that out-of-order execution is only done when the compiler/CPU can prove that the result will be the same as in-order. – Adam Oct 11 '13 at 22:35
  • Why are you asking? What do you want to achieve? – nosid Oct 11 '13 at 22:38
  • @Adam that could be an answer. – Alex Celeste Oct 11 '13 at 22:38
  • @nosid I want to achive portable way of ensuring order of execution and/or memory access order. As it turns out, comma is not that way :) – Amomum Oct 11 '13 at 22:44
  • @Amomum there is no guaranteed portable way at all. The closest you might get is assembly with a fence after every instruction, but even then some CPUs might find a way to re-order that. Plus "portable" assembly is a silly prospect. – Adam Oct 11 '13 at 22:50
  • 1
    @Amomum: C++11 supports multi-threaded programming. That means there is an easy and portable way to ensure the order of execution (with the _as-if_ rule). Just ask for it. – nosid Oct 11 '13 at 22:50
  • @Amomum the bigger question is, why do you want this guarantee? – Adam Oct 11 '13 at 22:50
  • @nosid and what is that way? – Amomum Oct 11 '13 at 22:53
  • @Adam mostly i want it for peripheral registers access. – Amomum Oct 11 '13 at 22:54
  • @Amomum: If you want a good answer, then make a new question on stackoverflow. Otherwise search for _Sequential Consistency for Data-Race-Free Programs_. There are also some good videos of presentations held by Hans Boehm. – nosid Oct 11 '13 at 22:58
  • @Amomum I figured it was hardware I/O, that's the only legitimate use case I can think of. Yeah, `volatile` is the only thing in the standard that I'm aware of the feeds into this, but the only standardized part of `volatile` is that it exists. But usually that's what it's used for. – Adam Oct 11 '13 at 22:58
  • @Adam i thought volatile only guarantees that value will actually be written into (or read from) the memory and will not be cached in the register. – Amomum Oct 11 '13 at 23:10
  • @Amomum yes, but doing so in effect turns off some optimizations. It's not a cure-all, but it's probably a piece of the puzzle. People better versed at dealing with hardware can probably help better than I can. – Adam Oct 11 '13 at 23:13
  • You should probably ask another question that's aimed at how to interface with hardware as opposed to how to force in-order execution. I bet you'd get better suggestions that way. – Adam Oct 11 '13 at 23:15
  • @Adam: The **As If Rule** allows the compiler to re-order instructions as long as they produce the same result as the normal order (which is basically what you said). But the important variant is that it does not take into account things like volatile memory and tricky resource (basic memory model is all the compiler has to consider (unless your compiler documents volatile to handle this kind of stuff)). – Martin York Oct 11 '13 at 23:34
  • @LokiAstari well said, I agree. – Adam Oct 11 '13 at 23:46

3 Answers3

7

The comma operator guarantees that the left side of the expression is evaluated before the right side of the expression within one thread. When the results are stored into memory is entirely unrelated to evaluation order, though, and requires some form of synchronization, e.g. memory barriers.

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
3

No, it will not. Order of execution (as measured by other threads) is guaranteed by the tools that are designed to guarantee order of execution. The comma operator is for discarding the value of the first expression, and is of limited use.

The "as if" rule is king here, and in the absence of barriers, it's construed in a single-threaded context.

Sneftel
  • 40,271
  • 12
  • 71
  • 104
3

The comma operator is no different from simply two statements separated by ; in this respect

The language specifies the semantics of the operator, but the compiler/CPU can choose how they want to implement it. If they can do things out-of-order they are free to, as long as they can prove that the result will be the same as in-order. And they do, often.

If you want guarantees about the actual order for some reason, then you'll have to check your compiler and CPU documentation for how to enforce it. That might mean turning off optimizations, using extra keywords like volatile, use memory fences, etc. However, unless you absolutely positively need in-order, let the compiler and CPU do their thing and give you extra performance at no extra cost to you.

Adam
  • 16,808
  • 7
  • 52
  • 98
  • 1
    http://stackoverflow.com/questions/2484980/why-is-volatile-not-considered-useful-in-multithreaded-c-or-c-programming – nosid Oct 11 '13 at 23:03
  • @nosid OP hasn't mentioned threading once. This is for interfacing with hardware. – Adam Oct 11 '13 at 23:10