In the new C++11
standard, many atomic operations are defined in "strong/weak" pairs:
template< class T >
bool atomic_compare_exchange_weak( std::atomic<T>* obj,
T* expected, T desired );
template< class T >
bool atomic_compare_exchange_weak( volatile std::atomic<T>* obj,
T* expected, T desired );
I understand that the weak one may be faster, but may fail occasionally, so one needs to put it into a while
loop like below (taken from cppreference):
void append(list* s, node* n)
{
node* head;
do {
head = s->head;
n->next = head;
} while(! std::atomic_compare_exchange_weak(s->head, head, n));
}
But
What exactly do the weak operations do? Why do they sometimes fail, and why are they faster? (I would love some hardcore microprocessor architecture details.)
When is the weak form preferable, and when is the strong one recommended?