Memcpy () security in contiguous memory areas

I recently asked a question about using volatile and was aimed at reading some very informative articles from Intel and others that discussed memory barriers and their use. After reading these articles, I became quite paranoid.

I have a 64-bit machine. Is memcpy safe in adjacent, non-overlapping memory areas from multiple threads? For example, let's say I have a buffer:

char buff[10];

Is it always safe for one memcpy stream in the first 5 bytes, and the second stream copies in the last 5 bytes?

My gut reaction (and some simple tests) show that it is absolutely safe, but I could not find the documentation anywhere, which could completely convince me.

+3
source share
4 answers

Safe, yes. Performer, no, in this limited example, at least. Remember that one cache line cannot be in two cores at the same time. You will make kernel A wait for kernel B to write to the buffer, and then wait for the memory to be transferred, and then write to it. Multi-core copies of memory must be very large in size to avoid this effect.

+6
source

Yes, it is absolutely safe; serialization of access to the memory bus is done in hardware.

+4
source

memcpy , , . ++ ; , , , , , . memcpy , , , . .

+2

, - . .

0

Source: https://habr.com/ru/post/1785517/


All Articles