Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is memcpy process-safe?

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?

like image 556
twerdster Avatar asked Feb 28 '13 20:02

twerdster


3 Answers

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);
like image 126
John Källén Avatar answered Oct 09 '22 11:10

John Källén


memcpy is not thread/process safe

like image 23
Drakosha Avatar answered Oct 09 '22 11:10

Drakosha


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.

like image 4
Roman Nikitchenko Avatar answered Oct 09 '22 13:10

Roman Nikitchenko