I'm working on a multi threaded program that provides access to one side of an interprocess communication system. Having never used volatile, I'm trying to figure out its proper usage.
I understand that (the relevant part) volatile tells the compiler that the variable it's applied to might be written to outside of the sequence of instructions of this thread so it should reread the memory every time it's used.
I have looked at some tutorials on volatile, but most either have the simplest examples (such as global shared variable) or just copy each other. Then from time to time I see someone positing that volatile doesn't do what you think it does. Also, some people say that if you're not writing device drivers or some such you shouldn't use volatile ( Is 'volatile' needed in this multi-threaded C++ code?). At this point I have my brain in a knot and I don't even know if the things that I worry about are real issues. So I'm wondering if there are any example code of volatile usage in OOP that I could look at.
But in particular, my main concern is about the external usage of getter function. In my code everything that I use is properly protected, however, I do provide some getter functions for private member variables that I don't protect (similar to this question Overhead of pthread mutexes?, especially the last paragraph of the first answer, or unprotected access to member in property get) because I don't want to tie my writing thread down, especially if multiple other threads are waiting in loops using the get function.
I'm worried that if I don't set the variable accessed with the get function as volatile then if some external program is waiting on it in a loop then the compiler for that external program might not reread the variable so the outside program will stay in an infinite loop. Is this a valid concern?
A very similar question is here, but I'm not sure what the answer was for c++.