atomic swap is implemented for synchronization.atomic
swap is implemented for synchronization.
- Atomic swap uses multithreading to achieve synchronization.The
content of memory location is compared with the multiple given
value or variable and if they are same then content of that memory
location is modified with a new given value.
- So here we can see that the variable be first compared with the
value to see if it has changed.
- The whole operation of comparing and swaping is called as
Atomic.
- Atomic swap is implemented for synchronization using thread, if
the thread is able to successfully swap it proceeds further
otherwise it has to retry.
- Coding Example of implementation --
- synchronized boolean compare_and_swap(integre var, int oldval,
int newval)
- {
- int old_reg_val = var;
- if(old_reg_val == oldval)
- {
- var = newval;
- return true; // indicates that swap is successful
- }
- return false; // indicates that swap is unsuccessful
- }