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
1 answer

Atomic instruction : How can other thread update the value while Compare and Swap instruction is in progress?

As per my understanding, any atomic instructions(compare_and_swap, test_and_test, fetch_and_add) are executed as a single instruction. Though they involve multiple CPU cycles/operations, it is invisible to the thread/process. If a thread is…
0
votes
1 answer

locking with compare and swap on a boolean

I am experimenting the atomic_compare_and_swap function to do a basic lock on a std::atomic. The behaviour I was expecting is that the second thread i.e Consume would remain blocked in the while loop at the beginning of Access::get() for all…
Abruzzo Forte e Gentile
  • 14,423
  • 28
  • 99
  • 173
0
votes
1 answer

compare row's if value of corresponding column's not equal insert them

i have two grouped of columns.one of them is subset of another.i want to write a macro compare them and if they were not equal insert second one to first one. algorithm ode: if (code-1)<>(code-2) AND (serial-1)<>(serial-2) AND (amount-1)<>(amount-2)…
shahram
  • 3
  • 7
0
votes
0 answers

Non-blocking algorithm to modify a List in Java

The use case requires me not to block any thread while retrieving or putting values into a List. I have been looking at compare-and-swap algorithms that require you to implement a datastructure yourself. I would like to use an existing…
user592748
  • 1,194
  • 3
  • 21
  • 45
0
votes
0 answers

Swapping imputed values. Z axis optimization. Making smallest value the Z axis

I am making a simple cost estimator. It takes 3 imputed values X,Y,Z and displays a price which is a simple calculation of a value by the z axis (the third of 3 collected values). You can view the estimator here:…
Mr Boom
  • 41
  • 3
  • 10
0
votes
1 answer

C++ Bubble Sort Negative Numbers

I created a array bubble sort function for integers that works perfectly with positive integers but it crashes when negative integers are used. The initial display function works but then it just freezes. I have tried a signed int array to no…
0
votes
0 answers

Customize compare function NSString?

I want to sort array of string similar http://stackoverflow.com/questions/16541314/sorting-an-array-of-string-with-custom-ordering in objective C, I think similar to http://docs.oracle.com/javase/tutorial/i18n/text/rule.htmlThanks you.
LViet
  • 113
  • 11
0
votes
1 answer

modifying array elements atomically using Intel TBB

I have a tree node structure as: struct node { unsigned long key; tbb::atomic lChild; tbb::atomic rChild; }; I would be doing compare_and_swap on lChild and…
arunmoezhi
  • 3,082
  • 6
  • 35
  • 54
0
votes
1 answer

Intel x86 assembly to compare and reset memory

On an Intel x86 processor, is it possible to compare one value with another at a particular memory location, resetting the memory if the compare succeeded without worrying about multi-thread/processor issues? I see the instruction CMPXCHG - would…
thel0rax
  • 35
  • 2
0
votes
1 answer

Determine if concurrency logic can be done using *only* CAS operations

For high-performance multi-threading system, is there a deterministic way/methodology to determine what concurrency logic can be done using only compare-and-swap a.k.a. atomic operations, what must use locks, semophones and/or barriers? My systems…
Alex Suo
  • 2,977
  • 1
  • 14
  • 22
0
votes
2 answers

Java compareAndSet to atomically update references in a BST

In a binary tree, I'm trying to atomically replace left child of parent to a new node. In the below method, pnode.left is pointing to node and I'm trying to change it to replaceNode. In line1, childPtr is pointing to pnode.left In line2, oldChildPtr…
0
votes
0 answers

How to sum the index's of a ArrayList

I have a program that I have been working on for quite awhile now. I am need to make a program that solves a user specified summation puzzle through backtracking. The user enters three separate strings, the first two strings added together should…
DLF85
  • 189
  • 4
  • 12
0
votes
2 answers

Is this a good design for implementing the java synchronized keyword as an object?

Just for practice I wanted to implement the java synchronized keyword as a java object. Would you say the code below is a good design for this? I guess AtomicReference would have a similar performance to AtomicBoolean? Updated code after…
newlogic
  • 807
  • 8
  • 25
0
votes
1 answer

Java: Forever waiting Thead

Consider the following execution statements: (1) Thread A: Checks for a specific lock state and fails (2) Thread A: Hence tries to go to wait state (3) Thread B : Done with a specific task and modified the lock state desired by Thread A (4) Thread B…
New Coder
  • 499
  • 4
  • 22
0
votes
1 answer

ConcurrentLinkQueue implementation going into deadlock

I am learning concurrent programming and wrote this concurrentLinkeQueue using AtomicReference. Following Example goes into Deadlock. Please see. package concurrent.AtomicE; import java.util.concurrent.atomic.AtomicReference; public class…
1 2 3
22
23