InterlockedIncrement increments (increases by one) the value of the specified 64-bit variable as an atomic operation.
InterLockedIncrement and InterLockedDecrement 实现数的原子性加减。什么是原子性的加减呢? 举个例子:如果一个变量 Long value =0; 首先说一下正常情况下的加减操作:value+=1; 1:系统从Value的空间取出值,并动态生成一个空间来存储取出来的值; 2:将取出来的值和1作加法,并且将和放回Value的空间覆盖掉.
Syntax
Parameters
Addend
A pointer to the variable to be incremented.
Return Value
The function returns the resulting incremented value.
Remarks
The variable pointed to by the Addend parameter must be aligned on a 64-bit boundary; otherwise, this function will behave unpredictably on multiprocessor x64 systems and any non-x64 systems.
The interlocked functions provide a simple mechanism for synchronizing access to a variable that is shared by multiple threads. This function is atomic with respect to calls to other interlocked functions. It is thread-safe.
This function is implemented using a compiler intrinsic where possible. This function generates a full memory barrier (or fence) to ensure that memory operations are completed in order.
Requirements
See Also:
Your example implementation is constructing a new atomic from a pointer each time. This is not the intended use of std::atomic and I do not believe it works how you would like.To my knowledge, the only way to do what you are looking to do (remove dependence on InterlockedIncrement in a platform independent way) is to replace all declarations for variables that you currently are calling Win32 'interlock' calls on with std::atomic versions of them. Then, you can remove the interlocked calls and use regular value semantics to modify the variable atomically. This is more readable (and more maintainable in the future), anyway.I understand you wish to leave existing (well tested) code in place but I don't think you can in your case.
Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
March 2023
Categories |