int val = memLoc[index++];
or better yet
int val = memLoc[index++ & 0xFF];
Trying to do a threadsafe read from a shared ring buffer where each call gets the next value – and I’d love it to be lock free if at all possible as it happens a TON. No Boost / C++ 11 allowed 🙁
The only operation here which needs to be synchronized is the increment of the
indexvalue. Since this is just a numeric value it can be done without the use of locks via an atomic increment. The rest of the operations you listed are just reads of a shared location and don’t need to be synchronized.On Win32 making the increment synchronized is done with the
InterlockedIncrementfunctionThere are various synchronized increment functions available on Linux. There is a fairly good discussion about the options on this stackoverflow thread