Questions tagged [compare-and-swap]

Compare and swap (compare and exchange) is an atomic operation that writes a value to a memory location only if its current value is equal to a given expected value.

Compare and swap (compare and exchange) is an atomic operation that writes a value to a memory location only if its current value is equal to a given expected value. CAS operations is often used in multithreading to achieve synchronization.

346 questions
7
votes
3 answers

Java CAS operation performs faster than C equivalent, why?

Here I have Java and C code that tries to do an Atomic increment operation using CAS. To increment an long variable from 0 to 500,000,000. C : Time taken : 7300ms Java : Time Taken : 2083ms Can any one double check these results? Because I just…
Jigar
  • 8,762
  • 6
  • 25
  • 26
7
votes
1 answer

MySQL Atomic UPDATE in InnoDB vs MyISAM

Is this "compare and swap" statement always atomic regardless of engine (e.g. InnoDB or MyISAM)? : UPDATE tbl_name SET locked=1 WHERE id=ID AND locked <> 1; I ask this because I intend to use this statement to do pseudo row-level locking that is…
mikegreiling
  • 1,160
  • 12
  • 21
6
votes
1 answer

compareandexchange() vs compareandset() of Atomic-Integer

While working on AtomicInteger, I found this API provides two Methods. compareAndExchange: Atomically sets the value to newValue if the current value, referred to as the witness value, == expectedValue, with memory effects as specified by…
T-Bag
  • 10,916
  • 3
  • 54
  • 118
6
votes
1 answer

C++ atomics memory ordering for some specific use case

I'm in the next situation where I use a atomic as a counter and increment it from 5 or more threads and use the value before increment to take some decision. atomic global_counter; void thread_funtion(){ uint64_t…
6
votes
1 answer

Why can the MESI protocol not guarantee atomicity of CMPXCHG on x86 without the LOCK prefix?

I understand that the MESI protocol successfully guarantees the same view of memory (caches) for different cores. My question comes from the fact that during writing MESI guarantees that the cache is exclusively owned by a CPU and then atomic…
6
votes
1 answer

Why do both free and member functions exist for compare and swap operations?

The C++ Standard Library has both free functions and member functions for atomic compare and swap operations. As noted for free functions: These functions are defined in terms of member functions of…
ks1322
  • 33,961
  • 14
  • 109
  • 164
6
votes
2 answers

Release-Only form of CAS

I have accidentally run into Striped64.java class from Kamon Monitoring tool. At line 95 I found this comment: JVM intrinsics note: It would be possible to use a release-only form of CAS here, if it were provided. Although I understand what CAS is,…
Vladimir G.
  • 418
  • 3
  • 10
6
votes
3 answers

atomic swap with CAS (using gcc sync builtins)

Can the compare-and-swap function be used to swap variables atomically? I'm using C/C++ via gcc on x86_64 RedHat Linux, specifically the __sync builtins. Example: int x = 0, y = 1; y = __sync_val_compare_and_swap(&x, x, y); I think this…
limalave
  • 91
  • 1
  • 2
  • 3
6
votes
1 answer

Purpose of CMPXCHG instruction without LOCK prefix?

I am reading Vol 3a of the Intel Developer manuals: http://www.intel.com/content/www/us/en/architecture-and-technology/64-ia-32-architectures-software-developer-vol-3a-part-1-manual.html and on page 245 it implies only XCHG instruction has automatic…
user997112
  • 29,025
  • 43
  • 182
  • 361
6
votes
1 answer

How can compare-and-swap be used for a wait-free mutual exclusion for any shared data structure?

Being new to multi-threading and mutexes I was going through the wikipedia for starters. I came across this portion: CAS can be used to achieve wait-free mutual exclusion for any shared data structure by creating a linked list where each node…
Akshya11235
  • 959
  • 4
  • 11
  • 25
6
votes
1 answer

How does incrementAndGet method of AtomicLong works internally?

I am using incrementAndGet method of AtomicLong in my multithreaded code to measure the performance of some of our client side code. @Override public void run() { long start = System.nanoTime(); attributes =…
arsenal
  • 23,366
  • 85
  • 225
  • 331
6
votes
5 answers

What is the difference between AtomicBoolean.set(flag) and AtomicBoolean.compareAndSet(!flag, flag)?

I wonder if there is any difference(or possible side effects) between calling: AtomicBoolean.set(true) and AtomicBoolean.compareAndset(false, true) The JavaDoc of AtomicBoolean#set states: Unconditionally sets to the given value. While…
ejboy
  • 4,081
  • 5
  • 30
  • 32
5
votes
1 answer

How to "mark" pointers by setting the last bit to 1?

I am trying to mark and unmark pointers so I can implement Non-Blocking linked lists. I checked that on my architecture the last bit is never used, so I am trying to use change it to mark/unmark pointers. I am trying to perform an OR to set the last…
5
votes
2 answers

Does cmpxchg write destination cache line on failure? If not, is it better than xchg for spinlock?

I assume simple spinlock that does not go to OS waiting for the purposes of this question. I see that simple spinlock is often implemented using lock xchg or lock bts instead of lock cmpxchg. But doesn't cmpxchg avoid writing the value if the…
Alex Guteniev
  • 12,039
  • 2
  • 34
  • 79
5
votes
2 answers

Why std::memory_order_relaxed is preferred at CAS loop when failing?

When it comes to implementing CAS Loop using std::atomic, cppreference in this link gives the following example for push: template class stack { std::atomic*> head; public: void push(const T& data) { node*…
Dean Seo
  • 5,486
  • 3
  • 30
  • 49