I implemented SpinLock class, as followed
struct Node {
int number;
std::atomic_bool latch;
void add() {
lock();
number++;
unlock();
}
void lock() {
bool unlatched = false;
while(!latch.compare_exchange_weak(unlatched, true, std::memory_order_acquire));
}
void unlock() {
latch.store(false , std::memory_order_release);
}
};
I implemented above class and made two threads which call add() method of a same instance of Node class 10 million times per thread.
the result is , unfortunately, not 20 million. What am I missing here?
See Question&Answers more detail:os