C ++ port for AtomicLong.lazySet

I am trying to port some Java code to Windows C ++ and am confused about how to implement AtomicLong.lazySet() . The only information I can find is that it does, but not how to implement it, and the available source code ends in a private native library owned by Sun ( sun.misc.Unsafe.class ).

I'm currently just setting a member variable to the passed parameter, but I'm not sure if this is correct.

 class AtomicLong { public: inline void LazySet(__int64 aValue) { // TODO: Is this correct? iValue = aValue; } inline void Set(__int64 aValue) { ::InterlockedExchange64(&iValue, aValue); } private: __declspec(align(64)) volatile __int64 iValue; }; 

I can not use boost.

Edit: I am compiling x64, but maybe solutions for 32-bit code will be useful to others.

I do not have access to C ++ 11.

+6
source share
1 answer

C ++ 11 contains an atomic library, and it's easy if you can use it:

 class AtomicLong { public: inline void LazySet(int64_t aValue) { iValue.store(aValue, std::memory_order_relaxed); } inline void Set(int64_t aValue) { iValue.store(aValue); } private: std::atomic<int64_t> iValue; }; 
+2
source

Source: https://habr.com/ru/post/922485/


All Articles