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
0
votes
2 answers

Compare-And-Swap not working on many Cores

When I discovered the "CAS" instruction, I remember that I well understood that it could work for threads running on one single CPU but I was surprised that it could for many CPUs Yesterday, I had my first opportunity to test it on one of my…
0
votes
1 answer

Trying to create CAS template

at the moment I'm busy fiddling with CAS operations and lock/wait-free algorithms, and for my own sanity I decided to implement a template to handling all the casting for me: VC6: template static inline T CAS(volatile T* pDest, T…
Necrolis
  • 25,836
  • 3
  • 63
  • 101
0
votes
1 answer

When should I favor a more specific atomic operation over using atomicCAS?

I have been using atomicCAS in a do-while loop to perform various arithmetic operations when needed in my first parallel programs. I see that there are other operations like atomicInc which would be the same thing as incrementing using atomicCAS in…
T Willy
  • 1
  • 1
0
votes
0 answers

X32 and __gnu_cxx::__exchange_and_add_single?

I'm trying to understand a crash in a library that's undergoing testing on X32 platform (x86_64 with ILP32). When I backtrace it, I see: (gdb) bt full #0 0xf6e02e1b in _Unwind_Resume () from /lib/x86_64-linux-gnux32/libgcc_s.so.1 No symbol table…
jww
  • 97,681
  • 90
  • 411
  • 885
0
votes
1 answer

Does the following lock-free code exhibit a race-condition?

Within the Kubernetes Go repo on Github.com, There is a lock-free implementation of a HighWaterMark data-structure. This code relies on atomic operations to achieve thread-safe code that is free of data races. // HighWaterMark is a thread-safe…
Ralph Caraveo
  • 10,025
  • 7
  • 40
  • 52
0
votes
1 answer

compare matrix elements with corresponding blocks in another matrix

I have a 4*4 matrix like X and a 2*2 matrix like A. X= x11 x21 x31 x41 A= 1 0 x12 x22 x32 x42 0 1 x13 x23 x33 x43 x14 x24 x34 x44 I divided X to four 2*2 blocks with the code…
J.Bea
  • 51
  • 1
  • 3
0
votes
1 answer

How to implement atomic_min?

I need to implement an atomic_min function, that is equivalent to: static void atomic_min(u64 *ptr, u64 value) { enter critical section *ptr = min(*ptr, value); exit critical section …
user1202136
  • 11,171
  • 4
  • 41
  • 62
0
votes
1 answer

Java AtomicInteger getAndIncrement() and compareAndSet

I am trying to understand how the current in getAndIncrement is updated, here is that piece of code. public final int getAndIncrement() { for (;;) { int current = get(); int next = current + 1; if (compareAndSet(current,…
Guifan Li
  • 1,543
  • 5
  • 14
  • 28
0
votes
1 answer

In Java 7 ConcurrentHashMap, why segment lock is required while writing? Why can't we use Unsafe again to keep things non-blocking?

While going through the internal implementation of Java 7 ConcurrentHashMap, I noticed that to set the new Segment, we are using the Unsafe class which does Ordered write and make use of Compare and Swap algorithm thus supporting non-blocking…
0
votes
2 answers

Not getting expected output using cmpxchg8b for unsigned long

I am trying to write a simple compare and swap inline assembly code. Here is my code #include #include #include static inline unsigned long cas(volatile unsigned long* ptr, unsigned long old, unsigned long _new) { …
Ritesh
  • 67
  • 8
0
votes
1 answer

How to implement atomic compare and swap using 'cmpxchg8b' assembly instruction in c?

I am trying to implement a lock free linked list. For that I need to implement atomic compare and swap instruction using inline assembly in C. I do know that I need to use the cmpxchg8b instruction for x86, however I am not able to do it. My node…
Sumant
  • 77
  • 6
0
votes
2 answers

Atomically decrement data member of a union?

I have a struct in a union with uint64_t, providing access to two int32_t. I would like to decrement one of the struct members atomically. I came up with this: class{ public: void atomicallyDecrement_B(const int32_t decrementByThisValue){ …
user997112
  • 29,025
  • 43
  • 182
  • 361
0
votes
1 answer

Lock free stack implementation in Python

Is it possible to implement lock free stack in Python? I have searched the web and didn't find a CAS function in Python.
Alex Gill
  • 179
  • 1
  • 8
0
votes
1 answer

Atomic operation functions for Linux 32bit and 64bit?

In Windows, atomic operation functions are separated by 32bit and 64bit like this: InterlockedIncrement32 InterlockedIncrement64 Meanwhile, what functions do the same in Linux? What is the function for doing 64-bit variable for…
Hyunjik Bae
  • 2,721
  • 2
  • 22
  • 32
0
votes
2 answers

C++ atomic on iOS

I have the next isolated test fragment inside an iOS project: /// ... std::atomic_bool ab; ab.store(true); bool expected = false; while (!ab.compare_exchange_weak(expected, true)); assert(0); // ... Provided that ab is not modified by other…
tonso
  • 1,760
  • 1
  • 11
  • 19