You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Ivan Kozhin edited this page Jun 19, 2020
·
4 revisions
Methods to performs atomic operations.
1. Swap
voidSwap<T>(refTitem1,refTitem2)
Swaps values of two variables.
inta=2,b=5;inttmp=a;a=b;b=tmp;// With Saritasa extensions.inta=2,b=5;AtomicUtils.Swap(refa,refb);
2. SafeSwap
voidSafeSwap<T>(refTitem1,refTitem2)
Swaps values of two variables. Thread-safe.
3. DoWithCAS
The thread-safe implementation CAS (Compare-And-Swap). Get the location variable from memory, perform an action on it and replace it. There are also override implementations for double and int. Here is an example of a thread-safe multiply method:
publicstaticvoidInterlockedMultiplyInPlace(refintx,inty){AtomicUtils.DoWithCAS(refx, t =>t*y);}