Say I have these 2 functions:
int* bigBuffer = new int[1024];
std::atomic<int> pos1 = 0;
volatile int pos2 = 0;
void push1(int value) {
int offset = pos1.fetch_add(1, std::memory_order_acq_rel);
bigBuffer[offset] = value;
}
void push2(int value) {
int offset = pos2++;
bigBuffer[offset] = value;
}
And then I have a multiple writer scenario where many threads call either push1 or push2 to store their data. The order of the data is not important, my main concern is speed.
From my limited testing, push2 is way faster and the data seems correct. Am I going to have problems with this down the road though?