473,385 Members | 1,740 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Question about CAS (compare and swap) method

CAS method can fail because of 2 reasons:
1. - the operation was failed because of other thread.
2. - the comparison was failed.

The question is: how I know the reason of failure? Because in first case I probably whould like to retry operation in loop until it succeeded, but in second case loop will be infinite.

Thanks.
Mar 7 '08 #1
5 3372
sukatoa
539 512MB
CAS method can fail because of 2 reasons:
1. - the operation was failed because of other thread.
2. - the comparison was failed.

The question is: how I know the reason of failure? Because in first case I probably whould like to retry operation in loop until it succeeded, but in second case loop will be infinite.

Thanks.
Welcome to this forum...

I really don't understand about your statements and the question...

I might be wrong if i will force myself to analyze your post...

Where did you get that "CAS"?

It is new to me...

Can you post what you have read about that term?


Wondering,
Sukatoa
Mar 7 '08 #2
JosAH
11,448 Expert 8TB
CAS method can fail because of 2 reasons:
1. - the operation was failed because of other thread.
2. - the comparison was failed.

The question is: how I know the reason of failure? Because in first case I probably whould like to retry operation in loop until it succeeded, but in second case loop will be infinite.

Thanks.
Look what Google came up with!

kind regards,

Jos
Mar 7 '08 #3
sukatoa
539 512MB
Base on Jo's URL given, It is about Assembly language program design implementation.

Correct me if im wrong...

I wonder why you post this in Java forum...
If you mean that you are asking this for Assembly, i can share a little bit
about XCHG and CMP instructions... Tell me...

But if you mean CAS in java (Compare and Swap)...

There are many possible ways, without failure(Depends on implementation)


Sukatoa...
Mar 8 '08 #4
Because you asking why I posting this question on Java forum, I feel I have to clarify number of points.
First, CAS operation is atomic compare-and-set(swap) operation. If you don't know what mean "CAS" - probably you can't help me.
So how this related to Java?
I talking about "java.util.concurrent" library. Please examine AtomicMarkableReference class, compareAndSet method. It implemented as the following:

public boolean compareAndSet(V expectedReference, V newReference, boolean expectedMark, ,boolean newMark)
{
ReferenceBooleanPair current = atomicRef.get();
return expectedReference == current.reference &&
expectedMark == current.bit &&
((newReference == current.reference && newMark == current.bit) ||
atomicRef.compareAndSet(current, new ReferenceBooleanPair<V>(newReference, newMark)));
}

So my previous question is related to last line in this method. Well, if we already talking about this, I will ask the main question:

When I looking to this implementation, I see 2 possible problems:

1. I think comparison operation (==) is not atomic, even on volatile values. To do comparison, CPU have to load first value to register, then second value to register and only then return result of comparison. Could other thread enter in the middle of comparison and change one of the values? In this case comparison operation could fail (or succeed) when it not supposed to.

2. Consider following history of thread A and thread B:

Thread A: ReferenceBooleanPair current = atomicRef.get(); //thread A obtained reference to volatile pair
Thread A: expectedReference == current.reference //here thread A completed first comparison
Thread B: ReferenceBooleanPair current = atomicRef.get(); //thread B preempted thread A
Thread B: expectedReference == current.reference &&
Thread B: expectedMark == current.bit &&
Thread B: ((newReference == current.reference &&
Thread B: newMark == current.bit)
Thread B: || CAS(current, new ReferenceBooleanPair<V>(newReference, newMark))) //thread B swapping 'current' reference with new one
Thread A: && expectedMark == current.bit ... //at this point, thread A doing comparison with different object - could lead to the same result as previous case (?). In literature this problem named as "ABA" problem.

So how this method is atomic?
Mar 8 '08 #5
sukatoa
539 512MB
Because you asking why I posting this question on Java forum, I feel I have to clarify number of points.
First, CAS operation is atomic compare-and-set(swap) operation. If you don't know what mean "CAS" - probably you can't help me.
So how this related to Java?
I talking about "java.util.concurrent" library. Please examine AtomicMarkableReference class, compareAndSet method. It implemented as the following:

public boolean compareAndSet(V expectedReference, V newReference, boolean expectedMark, ,boolean newMark)
{
ReferenceBooleanPair current = atomicRef.get();
return expectedReference == current.reference &&
expectedMark == current.bit &&
((newReference == current.reference && newMark == current.bit) ||
atomicRef.compareAndSet(current, new ReferenceBooleanPair<V>(newReference, newMark)));
}

So my previous question is related to last line in this method. Well, if we already talking about this, I will ask the main question:

When I looking to this implementation, I see 2 possible problems:

1. I think comparison operation (==) is not atomic, even on volatile values. To do comparison, CPU have to load first value to register, then second value to register and only then return result of comparison. Could other thread enter in the middle of comparison and change one of the values? In this case comparison operation could fail (or succeed) when it not supposed to.

2. Consider following history of thread A and thread B:

Thread A: ReferenceBooleanPair current = atomicRef.get(); //thread A obtained reference to volatile pair
Thread A: expectedReference == current.reference //here thread A completed first comparison
Thread B: ReferenceBooleanPair current = atomicRef.get(); //thread B preempted thread A
Thread B: expectedReference == current.reference &&
Thread B: expectedMark == current.bit &&
Thread B: ((newReference == current.reference &&
Thread B: newMark == current.bit)
Thread B: || CAS(current, new ReferenceBooleanPair<V>(newReference, newMark))) //thread B swapping 'current' reference with new one
Thread A: && expectedMark == current.bit ... //at this point, thread A doing comparison with different object - could lead to the same result as previous case (?). In literature this problem named as "ABA" problem.

So how this method is atomic?
Ok, i can't help you...
Please clarify your post next time...

I talking about "java.util.concurrent" library
Did you say this at the top? ;-)

To do comparison, CPU have to load first value to register, then second value to register and only then return result of comparison.
the Flag registers will be the reference of the result...
When compare, if equal, ZEROFLAG will be one... If less than, BORROWFLAG will be one, if above, CARRYFLAG will be one...

And the return is only the representation of the event happen during comparison...

That's all i can share...
Sukatoa...
Mar 8 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Randell D. | last post by:
Folks, I'm still learning javascript - I've invested in a couple of books and reading online as much as possible. I'm pretty sure what I am suggesting is possible though I'm trying to weigh up...
16
by: Kitty | last post by:
Hi, everyone. Given a vector<int>, what is the fastest way to find out whether there is a repeated element in it? The result is just "true" or "false". Thanks. Kitty
4
by: DancnDude | last post by:
I have a class that needs to have several different kinds of sorting routines on an ArrayList that it needs to conditionally do based upon the data. I have successfully created a class that...
9
by: Tim Rentsch | last post by:
I have a question about what ANSI C allows/requires in a particular context related to 'volatile'. Consider the following: volatile int x; int x_remainder_arg( int y ){ return x % y; }
29
by: Knut Olsen-Solberg | last post by:
I try to change the text in a <p> using getElementById(). I wonder what properties exists, and which one to use here. (The following does not work.) Regards Knut ______________________ ...
9
by: ma740988 | last post by:
Consider: # include <vector> # include <iostream> # include <cstdlib> # include <ctime> bool ispow2i ( double n ) {
30
by: gaoxtwarrior | last post by:
a sort which is stable means it keeps the object's original order. A sort which is in place is stable. does anyone can explain me what is sort in place? thx.
5
by: ciccio | last post by:
Hi, Again I am wondering about something. If you use the swap function of an std::vector, what does it do? Does it swap each element separately by means of copying to a tmp variable, or...
160
by: raphfrk | last post by:
Is this valid? int a; void *b; b = (void *)a; // b points to a b += 5*sizeof(*a); // b points to a a = 100;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.