Search code examples
cfwritevolatile

C - fwrite() from volatile source


I have some memory that a coprocessor can modify. I have marked my pointer to it as volatile, but if I use that memory as the source in an fwrite do I have to worry about linux caching (kernel postpones the copy until after the coprocessor has changed the data at that location) or anything else?

For example:

volatile My_Data_t* samples;
fwrite((void *) samples, 4, 1, fp);

Solution

  • Short answer is "yes".

    volatile doesn't add any extra protection to concurrency issues. It's just a hint to the compiler not to cache the contents in a register between successive accesses.

    I don't know the layout of a My_Data_t type, but you are writing at least four of them out at a time. If you are saying the co-processor can modify this array at any time, it's quite possible that during the fwrite call, a subset of the items in your array are modified.

    Does your co-processor have any capability of respecting a "lock" mechanism? That's ultimately what you need.