Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++11: what's the difference between atomic<T>::store and atomic_store<T>

One is a member function of template class std::atomic, one is template function, seems they do the same thing. As std is a class library, why it provides both class and none-class version of, I think the same operation?

Is there any real differences between them?

like image 537
Troskyvs Avatar asked Jul 26 '17 18:07

Troskyvs


2 Answers

There's no difference in semantics. The free functions were an attempt to achieve source compatibility with C11:

#ifdef __cplusplus
#include <atomic>
#define _Atomic(X) std::atomic<X>
#else
#include <stdatomic.h>
#endif

_Atomic(int) c;

int get_c(void) { 
    return atomic_load(&c); 
}
like image 116
T.C. Avatar answered Oct 26 '22 23:10

T.C.


Just like you said - one is a class, another is a function. Class have the interface - atomic<T> would provide for stores, loads, proper constructors, etc.

On the other hand, atomic_store could be specialized for your types.

like image 27
SergeyA Avatar answered Oct 27 '22 01:10

SergeyA