Code: Select all
; Define atomic functions for different platforms (Windows)
Procedure AtomicAdd(*Variable, Value)
*Variable = InterlockedExchangeAdd_(*Variable, Value)
EndProcedure
Procedure AtomicSub(*Variable, Value)
*Variable = InterlockedExchangeAdd_(*Variable, -Value)
EndProcedure
Procedure CAS(*Variable, Expected, NewValue)
*Variable = InterlockedCompareExchange_(*Variable, NewValue, Expected)
EndProcedure
; Testing atomic operations
Global AtomicCounter = 0
Global AtomicValue = 0
Procedure TestAtomicOperations()
; Perform Atomic Add
AtomicAdd(@AtomicCounter, 10)
Debug "After AtomicAdd: " + Str(AtomicCounter)
; Perform Atomic Sub
AtomicSub(@AtomicCounter, 5)
Debug "After AtomicSub: " + Str(AtomicCounter)
; Perform CAS (Compare-And-Swap)
CAS(@AtomicValue, 0, 123) ; If value is 0, swap it with 123
Debug "After CAS (Expected 0, NewValue 123): " + Str(AtomicValue)
CAS(@AtomicValue, 123, 456) ; If value is 123, swap it with 456
Debug "After CAS (Expected 123, NewValue 456): " + Str(AtomicValue)
EndProcedure
TestAtomicOperations()
https://learn.microsoft.com/en-us/windo ... xchangeadd
https://learn.microsoft.com/en-us/windo ... reexchange
// Moved from "Bugs - Windows" to "Coding Questions" (Kiffi)


