Write to the same memory address simultaneously

If two streams try to write to the same address at the same time, is the value after the parallel write guaranteed as one of the values ​​that the streams tried to write? Or can I get a combination of bits?

Also, is it possible for another thread to read the memory address when the bits are in an unstable state?

I think the question boils down to the fact that reading or writing to a single memory address is atomic at the hardware level.

+6
parallel-processing memory
source share
5 answers

Of course, for a data type of size equal to the CPU register, there can never be a bit in an unstable state, it will be one of two values

0
source share

I think it all depends on the “memory model" for your particular programming language or system.

+5
source share

These questions are the foundations of a system or / or memory model of a programming language. So choose your own OS and programming language, read the specifications, and you will see.

+2
source share

In some cases, the results can be just as unpredictable when two streams are written to different memory addresses - in particular, they think about the structures of the bit field C, as well as optimizing the compiler when writing to neighboring addresses.

If you enjoy reading, Boehm's “ Threads Cannot Be Implemented As a Library ” covers this and other concurrency quirks.

+1
source share

A multiprocessor computer may not have one “value” that is read. Two streams, and the third, can see inconsistent values. You will need a memory barrier so that each thread sees the same value at this address.

In addition, records are usually atomic, so this will be either one or the other of the values ​​that were written (or were there first) that are read. You are not talking about the Alpha processor, are you?

0
source share

All Articles