I'm having a bit of trouble with this bit:
volatile uint32_t chunk_size;
volatile T* chunk;
std::vector<volatile T*>* chunks;
CRITICAL_SECTION cs;
...
void push(T item) {
EnterCriticalSection(&cs);
uint32_t slot = chunk_size++;
if (slot == chunk_capacity) {
chunk = new T[chunk_capacity];
chunks->push_back(chunk);
chunk_size = 1;
slot = 0;
}
chunk[slot] = item;
LeaveCriticalSection(&cs);
}
So that should be a simple thread safe push.
I think T* chunk needs to be volatile because I don't want any thread caching the value since one of them might reallocate and change it.
The vector itself shouldn't need to be volatile since even though its contents change, the object itself doesn't move.
The vectors template type I'm not sure about, on one hand c++ won't let me push volatile types so I'd have to cast it to non-volatile. At the same time though, it is also a multiple consumer container and when one of them pops the container, it clears the vector. So if the address of chunks[2] were to be cached for instance, things will definitely go wrong.
So, should the vectors type template be volatile or not?