Is memcpy process-safe?
Solution 1
memcpy is typically coded for raw speed. It will not be thread safe. If you require this, you need to perform the memcpy call inside of a critical section or use some other semaphor mechanism.
take_mutex(&mutex);
memcpy(dst, src, count);
yield_mutex(&mutex);
Solution 2
memcpy is not thread/process safe
Solution 3
Routines like memcpy() (or memmove()) are part of standard C library, are included through standard <string.h> header and know nothing about any locking mechanics. Locking should be provided by some external way like inter-process mutexes, semaphores or things like this.
Solution 4
You are confusing "atomic" and "thread safe". If you read and write data (with or without memcpy
) concurrently in a shared region, that is not safe. But of course copying data itself is thread safe.
memcpy
itself is also thread safe, at least on POSIX systems see this one, and therefore I guess it is also on Windows. Anything else would make it quite useless.
If it would be "automatically blocking", it would have to be atomic (or at least manage it's own locks) which would slow down your system. So in your case you should use your own locks.
twerdster
Updated on July 28, 2022Comments
-
twerdster over 1 year
Ive looked online and have not been able to satisfy myself with an answer.
Is memcpy threadsafe? (in Windows)
What I mean is if I write to an area of memory shared between processes (using boost::shared_memory_object) using a single memcpy and then try read that area from another process using a single memcpy then will one process be blocked automatically while that write is happening? Where can I read about this?
-
not-a-user over 10 yearsYes you should use your own locking. But because you are operating on shared memory, not because
memcpy
is not thread safe. -
not-a-user over 10 yearsInteresting, I would have guessed the contrary. Do you have a reference?
-
Drakosha over 10 years@temple: nothing in the manual says it is, so it is not :) linux.die.net/man/3/memcpy
-
Mustafa Ozturk about 10 yearsThis answer is incorrect. In Windows there are C runtime libraries that are threadsafe. memcpy is threadsafe under Solaris.
-
John Källén about 10 yearsThe answer is correct. If you look at the source code of the MSVC runtime, you'll see very clearly that memcpy() does no synchronization. Just because Solaris happens to have a thread-safe memcpy() doesn't mean all implementations will, since it is unspecified whether memcpy() is thread-safe or not. Therefore, any programmer worth his salt will assume memcpy() is not thread-safe, and guard it with mutexes.
-
gnasher729 about 10 yearsThere's confusion between "thread safe" and "thread safe". You can call memcpy from different threads without problems. However, you can abuse the thread-safe memcpy to create data races, and then all odds are off. And that's apparently what the caller intends to do.
-
not-a-user about 8 yearsBy the way, what is "process safe" anyway? I should think it means, that I can call
memcpy
in two different processes at the same time. And that should be trivially true, because different processes actually have their own copy ofmemcpy
as much as they have their own copy of any other function. (Or at least the OS guarantees, in case of a shared C library, that the behavior is the same as if each process had its own copy.) -
not-a-user about 8 yearsActually the unsafe thing is accessing shared resources, like accessing a file from different processes - but that does not mean that
open
,read
, andwrite
are not process safe. (How would I signalinit
orexplorer.exe
that I'm going to callwrite
now, anyway?) -
Drakosha about 8 years@not-a-user: look at the original question and the answers. That will explain what they meant by thread/process safe. You are right about resources, that's indeed the problem :).