0

How can I do the following operations in single atomic operation? Is that possible?

 LARGE_INTEGER* ptr; // field

 void method()
 {
       LARGE_INTEGER* local = ptr;
       ptr = nullptr;
 }

So I want to store pointer from field into local pointer and immediately set that field to nullptr.

In other words, I want to move pointer from field into local variable in single atomic operation.

dlmeetei
  • 9,905
  • 3
  • 31
  • 38
M.kazem Akhgary
  • 18,645
  • 8
  • 57
  • 118

1 Answers1

2

Since C++11 you can use std::atomic for this purpose like this:

#include <atomic>
LARGE_INTEGER value;
std::atomic<LARGE_INTEGER*> ptr{&value};
LARGE_INTEGER* local = ptr.exchange(nullptr);
Dev Null
  • 4,731
  • 1
  • 30
  • 46