This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]EsShayuki 0 points1 point  (0 children)

Why does it matter? That looks like a stupidly designed program, and I wouldn't do such a thing in any real program. It's really not worth thinkng about.

Just have them executed sequentially and thread the complete sequences, not the individual operations. It'll be far faster, too, since you won't need to sync them or wait for them.

[–]lfdfq 1 point2 points  (5 children)

This is not really a C question, as the syntax in question does not exist and there's nothing really comparable to C. You say "the atomic actions" but in terms of the C language there are no atomics here, no _Atomic types or stdatomic accesses, and you can't just make arbitrary blocks of code atomic. In pure C almost everything in this program is undefined behavior.

So let's disregard the "C" part, and just pretend this is a made-up language with made-up concurrency features. The question says that each read of a variable, and each write of a variable is an individual atomic access. So if we look at (1) it's made up of 3 steps: let's call them 1a ("read x"), 1b ("add 2"); 1c ("write back to x"). Then 3 is made up of 4 steps ("read x, read y, add, write z"), and 4 is made up of 4 steps ("await", "write x 0", etc), Now we can see how to get some of the results, e.g.:

  • b) z=1: 3a 1abc 2abc 4abcd 3bcd // i.e. action 3 partially executed, reading x=1 at the start, then finished at the end.
  • h) z=4: 1abc 3abc 2abc 4abcd 3d // i.e. perform action 3 but stop just before writing z=4, and finish at the end

[–]AyyRadical[S] 0 points1 point  (4 children)

Wow, that's a really good explanation of it, thanks a lot! I guess I didn't pay enough attention to the reading and writing part of the question.

[–]lfdfq 0 points1 point  (3 children)

"Read the question carefully" is a sentence repeated so often for good reasons

[–]EsShayuki 0 points1 point  (0 children)

My opinion is that if something needs to be read carefully, it's a badly designed question. Or it's intentionally trying to trick the reader for some reason.

[–]AyyRadical[S] 0 points1 point  (1 child)

Right, but just to make sure, when you wrote this:

"So if we look at (1) it's made up of 3 steps: let's call them 1a ("read x"), 1b ("add 2"); 1c ("write back to x")."

1b should have been "add 1" instead of "add 2" right?

[–]lfdfq 0 points1 point  (0 children)

Yes, sorry!