compare-and-swap

How to implement prioritized lock with only compare_and_swap?

Given only compare and swap, I know how to implement a lock. However, how do I implement a spin lock 1) multiple threads can block on it while trying to lock 2) and then the threads are un-blocked (and acquire the lock) in the order that they blocked on it? Is it even possible? If not, what other primitives do I need? If so, how do I...

Can competing atomic operations starve one another?

Imagine a program with two threads. They are running the following code (CAS refers to Compare and Swap): // Visible to both threads static int test; // Run by thread A void foo() { // Check if value is 'test' and swap in 0xdeadbeef while(!CAS(&test, test, 0xdeadbeef)) {} } // Run by thread B void bar() { while(1) { ...

How are condition varaibles implemnted?

This has baffled me for a long time. Given basic atomic primitives like compare & swap, I can see how to implement a spin lock (from which I can build mutexes). However, I don't see how to build condition variables out of this. How is this done? Thanks! ...

AtomicSwap instead of AtomicCompareAndSwap ?

I know that on MacOSX / PosiX systems, there is atomic-compare-and-swap for C/C++ code via g++. However, I don't need the compare -- I just want to atomically swap two values. Is there an atomic swap operation available? [Everythign I can find is atomic_compare_and_swap ... and I just want to do the swap, without comparing]. Thanks! ...

Is OSCompareAndSwap (Mac OS X) equivalent to CMPXCHG8B?

Is OSCompareAndSwap (Mac OS X) equivalent to CMPXCHG8B? ...

Is OSCompareAndSwap is immune to ABA problem like CMPXCHG8B?

Is OSCompareAndSwap is immune to ABA problem like CMPXCHG8B? ...

Java Concurrency: CAS vs Locking

Im currently reading the Book Java Concurrency in Practice. In the Chapter 15 they are speaking about the Nonblocking algorithms and the compare-and-swap (CAS) Method. It is written that the CAS perform much better than the Locking Methods. I want to ask the people which already worked with both of this concepts and would like to hear ...

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 boils down to whether x can change between &x and x; for instance, if &x constitutes an op...

In Java what is the performance of AtomicInteger compareAndSet() versus synchronized keyword?

I was implementing a FIFO queue of requests instances (preallocated request objects for speed) and started with using the "synchronized" keyword on the add method. The method was quite short (check if room in fixed size buffer, then add value to array). Using visualVM it appeared the thread was blocking more often than I liked ("monito...

Fetch-and-add using OpenMP atomic operations

I’m using OpenMP and need to use the fetch-and-add operation. However, OpenMP doesn’t provide an appropriate directive/call. I’d like to preserve maximum portability, hence I don’t want to rely on compiler intrinsics. Rather, I’m searching for a way to harness OpenMP’s atomic operations to implement this but I’ve hit a dead end. Can thi...