2

Usually in C, we right statements as a list, and when the program is run, it executes the statements one by one. Is it possible to make two statements be executed simultaneously?

For example, suppose I wish to swap two variables a and b. Usually we would declare a third variable c.

c=b;
b=a;
a=b;

But if we were capable of simultaneously executing two statements, then we wouldn't need a third variable c. We could do a=b; and b=a; simultaneously instead.

So, is there a way to simultaneously execute two or more statements at the same time?

gsamaras
  • 71,951
  • 46
  • 188
  • 305
Pritt Balagopal
  • 1,476
  • 2
  • 18
  • 32
  • Nope you cannot simultaneously swap the two variables, as there are only two memory locations involved, one for a one for b. If you consider a CPU register as intermediate - that's like using a 3rd variable. Doing so with two threads for instance is undefined behavior. – Déjà vu Nov 23 '17 at 08:43
  • 2
    Did you bother looking at generated assembly? Compilers are fairly efficient at recognizing swaps and optimizing them. Don't do premature optimization. – StoryTeller - Unslander Monica Nov 23 '17 at 08:44
  • Only if they are `register` variables, and the processor has an instruction to exchange registers, but I don't think C will do this. AFAIK processors don't have memory exchange instructions. You can use the [`XOR` method](https://stackoverflow.com/questions/36906/what-is-the-fastest-way-to-swap-values-in-c) which does not use a third variable, but it is not very efficient, and cannot be done simultaneously. – Weather Vane Nov 23 '17 at 08:50
  • 1
    FWIW the Intel x86 family has an `XCHG` instruction which can swap two integers in one operation. – Paul R Nov 23 '17 at 08:50
  • @PaulR yes, but it implies LOCK. So it is actually slower – bolov Nov 23 '17 at 08:53
  • @bolov: yes, I only mention it because it achieves what the OP is asking for in the case of swapping two integers simultaneously. – Paul R Nov 23 '17 at 08:54

7 Answers7

9

The statement "when the program is run, it executes the statements one by one" shows that you're fundamentally misunderstanding the C programming language.

The C standard says that the compiled program needs to be executed so that the side effects of the program happen in order as if these statements were executed in the C abstract machine according to the rules of the abstract machine. However assigning a value to a non-volatile variable does not count as such a side effect, i.e. in your program

c = b;
b = a;
a = c; // notice, it is *c* here

since none of the statements have any visibile side effect the compiler is free to reorganize, interleave and eliminate these statements for as long as it does not change any previous or following side effect or their relative ordering.

In practice any decent compiler will notice that this is a swap operation and it would encode this with one assembler opcode, such as the x86 XCHG even though the C programming language does not have a swap operation in itself. Or, it could be that these generate zero opcodes and the compiler just remembers that "henceforth b shall be known as a and a as b."

The only way to actually force the compiler to generate a program that would execute each of these statements strictly sequentially would be if each of the statements touch a variable that is volatile-qualified, because accessing a volatile qualified object is considered a side effect.

4

You can create multiple threads in C (using libpthreads) and if you have a multi-core CPU the threads may get executed simultaneously.

The issue with your example is that the data depends on each other. You will create a race condition.

If you want to swap two variables without an intermediate variable you can use the XOR swap algorithm, but it's less efficient than simply using an intermediate variable.

neuhaus
  • 3,886
  • 1
  • 10
  • 27
  • Threads will generate race conditions for what the OP is asking for. – selbie Nov 23 '17 at 08:44
  • 1
    [The XOR swap hack is inefficient, unreliable, and should be discouraged.](https://stackoverflow.com/q/10549155/253056). – Paul R Nov 23 '17 at 08:47
3

There is multithreading but whatever you said is not possible because there is data dependency between this two statements.

Simultaneously executing the 2 will never give a plausible result.

What you can do is identify different independent section of program and then execute them in different threads. That's the level of parallelism you can achieve from the programmer's perspective.

user2736738
  • 30,591
  • 5
  • 42
  • 56
1

So, is there a way to simultaneously execute two or more statements at the same time?

Yes, by multithreading.

Even though however, you need to have two threads run at the same time, in order to achieve that effect.

In general, we don't quest for statements to be executed simultaneously though, it's too hard, and the gain from it simply doesn't worth it.


In your case however, it would cause data races.


PS: You can swap the numbers without a temporary value, like I describe here.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
1

Statements in c are executed sequentially unless and until you use break or continue labels in your code. You cant execute statements simultaneously specially in example you have specified. If you don't want to use temporary variable then you can use this logic.

a = a+b;
a = a-b;
b = a-b;
Cs Student
  • 61
  • 1
  • 7
0

With SIMD instructions multiple statements can be executed as simultaneously as parallel (vectorized) if your data fits.

Example:

a = e + g * ...
b = f + g * ...
c = e + g * ...
d = f + g * ...
recp
  • 383
  • 1
  • 2
  • 14
0

No matter how you slice it, the CPU will have to preserve the value of b in a temporary location before it gets overwritten with a and so it can be used again in the assignment back to the other variable. Even languages such as Python that allow you say a,b=b,a are really generating those temp variables under the hood.

There's a few folks mentioning "threads" in other answers. At this point, given the nature of the question you are asking, I would strongly not recommend you pursue that. (You aren't ready for threads!) It's highly unusual to use threads to do a simple variable swap and will only be slower with more race conditions to account for.

If you're looking for a shorthand way to express a "swap", you can always define your own macro.

#define SWAP(x,y) {auto tmp=x; x=y; y=tmp;}

Then you could just say: SWAP(a,b) with your own code.

selbie
  • 100,020
  • 15
  • 103
  • 173