0

I read webrtc source code, I find this code

 class AtomicOps {
 public:
  // Assumes sizeof(int) == sizeof(LONG), which it is on Win32 and Win64.
  static int Increment(volatile int* i) {
    return ::InterlockedIncrement(reinterpret_cast<volatile LONG*>(i));
  }
  static int Decrement(volatile int* i) {
    return ::InterlockedDecrement(reinterpret_cast<volatile LONG*>(i));
  }
  static int AcquireLoad(volatile const int* i) {
    return *i;
  }
  static void ReleaseStore(volatile int* i, int value) {
    *i = value;
  }
  static int CompareAndSwap(volatile int* i, int old_value, int new_value) {
    return ::InterlockedCompareExchange(reinterpret_cast<volatile LONG*>(i),
                                        new_value,
                                        old_value);
  }
  // Pointer variants.
  template <typename T>
  static T* AcquireLoadPtr(T* volatile* ptr) {
    return *ptr;
  }
  template <typename T>
  static T* CompareAndSwapPtr(T* volatile* ptr, T* old_value, T* new_value) {
    return static_cast<T*>(::InterlockedCompareExchangePointer(
        reinterpret_cast<PVOID volatile*>(ptr), new_value, old_value));
  }
};

I think static int AcquireLoad(volatile const int* i) function does not have atomic property? Is this right? if I am right, why use volatile ?

Sefe
  • 13,731
  • 5
  • 42
  • 55
funs
  • 137
  • 1
  • 9
  • 5
    `volatile` have nothing to do with atomicity or synchronization. Please read more about it in e.g. [this reference](http://en.cppreference.com/w/cpp/language/cv). Or ["Why do we use volatile keyword in C++?"](http://stackoverflow.com/questions/4437527/why-do-we-use-volatile-keyword-in-c). Or ["Why does volatile exist?"](http://stackoverflow.com/questions/72552/why-does-volatile-exist). Or a hundred of other questions here on SO, or thousands all over the web. – Some programmer dude Jan 11 '17 at 06:32
  • @Someprogrammerdude yes, I know that volatile have no atomicity. But, I think the class AtomicOp have some "atomicity". – funs Jan 11 '17 at 06:37
  • 2
    Yes, but not through the use of `volatile`. I suggest instead you read about [the `InterlockedCompareExchange` function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms683560(v=vs.85).aspx) and its siblings and what they do. – Some programmer dude Jan 11 '17 at 06:47

0 Answers0