When manipulating different indexes of an array in C / C ++ with two streams, is synchronization necessary?

Suppose I have an array defined as follows:

volatile char v[2]; 

And I have two threads (denoted respectively by A, B) controlling the v array. If I guarantee that A, B use different indexes at any time, that is, if A now manipulates v[i] , then B does nothing or does not manipulate v[1-i] . I wonder if synchronization is needed for this situation?

I mentioned this question , however I think it is limited to Java. The reason I ask this question is because I am struggling to deal with a strange and rare mistake in a large project for several days, and so far the only reason I could explain this error , lies in the fact that synchronization is necessary for the above manipulation. (Since error is very rare, it is difficult for me to prove whether my hypothesis is true)

Edit: both reading and changing are possible for v .

+10
c ++ c arrays multithreading synchronization
Sep 06 '14 at 5:16
source share
3 answers

This could be a compiler error or a hardware limitation.

Sometimes, when the memory is accessed by a less than 32-bit / 64-bit variable, the processor reads 32 bits, sets the corresponding 8 or 16 bits, and then writes the entire register. This means that it will read and write neighboring memory, which leads to data race.

Solutions

  • use byte access instructions. They may not be available for your processor, or your compiler does not know how to use them.

  • enter your items to avoid this kind of sharing. The compiler should do this automatically if your target platform does not support byte access. But in the array, this conflicts with the remarks of memory.

  • synchronize the whole structure

Discussion C ++ 03 / C ++ 11

In classic C ++, this is for you to avoid / mitigate this behavior. In C ++ 11, this violates the details of the memry model, as stated in other answers.

+2
06 Sep '14 at 5:34
source share
β€” -

Regarding C ++ 11 and C11 standards, your code is safe. C ++ 11 Β§1.7 [intro.memory] / p2, note not taken into account:

A memory location is either an object of a scalar type or a maximum sequence of adjacent bit fields having a nonzero width. Two or more threads of execution (1.10) can update and access separate memory without interfering with each other.

char is an integral type, which means its arithmetic type, which means that volatile char is a scalar type, so v[0] and v[1] are separate memory cells.

C11 has a similar definition in Β§ 3.4.

Before C ++ 11 and C11, the language itself does not have the concept of threads, so you remain at the mercy of the specific implementation that you use.

+7
06 Sep '14 at 5:39
source share

You need to handle synchronization only if you access the same memory and change it. If you are just reading, you also don't need to worry about synchronization.

As you say, each thread will have access to different indexes, then you will not need synchronization here. but you need to make sure that two threads should not change the same indicator at the same time.

0
Sep 06 '14 at 5:23
source share



All Articles