Global data:
uint16_t global_buffer[128];
Thread 1:
uint16_t local_buffer[128];
while(true)
{
...
if(data_ready)
memcpy(global_buffer, local_buffer, sizeof(uint16_t)*128);
}
Thread 2:
void timer_handler()
{
uint16_t value = global_buffer[10];
//do something with value
}
My question is whether this is safe to do? I mean, is it guaranteed that value will either get an old value or a new value (if thread 1 memcpy() is interrupted by context switch)?
Is it possible that the memcpy gets interrupted after one byte of the 16-bit value is updated but not the second. In that case, value will be garbage.
If memcpy operation only gets interrupted in between blocks of even number of bytes, I think this is safe.
Platforms: x86 & x86-64 only (only Intel i7 processor or newer actually)
OS: Linux
Compiler: gcc