Part Number: TM4C1294NCPDT
Tool/software: TI C/C++ Compiler
I have a large global data structure that is shared between two threads of execution (I'm using TI-RTOS, but the threads might as well be ISRs), in producer-consumer fashion, and want to use a flag to indicate that the data is ready to be read. I always assumed all I'd have to do is declare the flag as volatile and everything would work. However, my understanding of this answer is that this is incorrect: while the writes and reads to the flag are guaranteed to be consistent, I can't guarantee their ordering with respect to reads and writes to the global data structure. The only way to guarantee that this ordering will be preserved is to declare the large data structure as volatile.
This, however, is very awkward if the large data structure is a class, since C++ does not provide default constructors and assignmentfor volatiles (so I have to define these manually for all types used by my class, effectively punishing me for using OO). So, it seems the correct solution is to implement some sort of memory fencing. The linked topic mentions that disabling interrupts implements a full memory barrier, so I'm very tempted to just use a __disable_irq() something followed by __enable_irq() to avoid reordering. Would that work? Is there a better solution?