开发者

Does concurrent READ/WRITE access to memory by itself cause errors/violations?

If I have a multiprocessor setup and two threads that access the same memory (let's say the same actual byte or word, not only "the same area"), does this cause errors by itself?

Wh开发者_运维知识库at if two threads are not only reading, but also writing and combining reading and writing (say, thread 1 tries to read at the same time as thread 2 is trying to write, or if both are trying to write at the same time). Will this cause an error/BSOD/AV, or is the only problem that the behaviour is undefined? (that one of the threads will get wrong data, dependent on the actual timings)


1) No, threads are free to read/write everywhere in program application memory. (Ok, it is possible to protect some part of memory, like program code memory to protect it.)

2) Any CPU core has own cache and data is first copied and than modified in cache line after that is copied at appropriate (unpredictable) time back to RAM. There are special cpu instruction (like lock) which must be executed together with other instruction (like cmpxchg) to ensure atomic (interlocked) RAM read, write or modify, some instructions are atomic by default.

Remember: the atomic (interlocked) RAM access can be only 1, 2, 4 or 8 (and 16 under 64bit CPU) bytes long. For longer memory structures you must ensure corresponding locking (sinhronisation) mechanism like Critical Section to avoid uncontroled memory access by several threads.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜