I'm trying to port some Java code to Windows C++ and am confused about how to implement AtomicLong.lazySet()
. The only information I can find talks about what it does but not how to implement it, and the available source code ends up in a private native library owned by Sun (sun.misc.Unsafe.class
).
I currently just set a member variable to the passed parameter but I'm not sure if it's 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 cannot use boost.
Edit: I'm compiling to x64 but perhaps solutions for 32-bit code would be of help to others.
I don't have access to C++11.
C++11 contains an atomic library, and it is 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;
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With