compare-and-swap

atomic swap with CAS (using gcc sync builtins)

前提是你 提交于 2019-12-04 08:18:41
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 operation, it might be possible for x to change between &x and x in the arguments. I want to assume that the comparison implicit above will always be true; my question is whether I can. Obviously there's the bool version of CAS, but then I can't get the old x to write into y

Java Atomic Variable set() vs compareAndSet()

那年仲夏 提交于 2019-12-04 05:01:47
I want to know the difference between set() and compareAndSet() in atomic classes. Does the set() method also ensure the atomic process? For example this code: public class sampleAtomic{ private static AtomicLong id = new AtomicLong(0); public void setWithSet(long newValue){ id.set(newValue); } public void setWithCompareAndSet(long newValue){ long oldVal; do{ oldVal = id.get(); } while(!id.compareAndGet(oldVal,newValue) } } Are the two methods identical? The set and compareAndSet methods act differently: compareAndSet : Atomically sets the value to the given updated value if the current value

In what situations can do-while be more efficient than while?

余生颓废 提交于 2019-12-04 03:53:47
While vs. do-while While and do-while are functionally equivalent when the blocks are empty , although while seems more natural: do {} while (keepLooping()); while (keepLooping()) {} One typical use case of a while/do-while with an empty block is to force an update of atomic objects with a compareAndSet (CAS). For example the code below will increment a in a thread-safe way: int i; AtomicInteger a = new AtomicInteger(); while (!a.compareAndSet(i = a.get(), i + 1)) {} Context Several parts of java.util.concurrent use the do {} while (...) idiom for CAS operations and the javadoc of ForkJoinPool

Why do C++11 CAS operations take two pointer parameters?

谁都会走 提交于 2019-12-03 23:49:17
Many of the C++11 CAS operations (e.g., atomic_compare_exchange_weak , atomic_compare_exchange_strong ) take two pointers and a value, i.e., like this: bool atomic_compare_exchange(T* pointer, T* expected, // pseudodeclaration! T desired); In contrast, the CAS operations from Microsoft, gcc, and Intel all take one pointer and two values: long InterlockedCompareExchange(long* pointer, long desired, // Microsoft long expected); int __sync_bool_compare_and_swap (T* pointer, T expected, // gcc and T desired); // Intel Why do the C++11 CAS functions take two pointers and a value instead of what

Why do we need to compareAndSet when set is already atomic in Java?

我是研究僧i 提交于 2019-12-03 19:29:50
Since Atomic means thread safe. When do we ever use compareAndSet when .set() itself is Atomic and thread safe in java? say for example I want to set a variable atomically such that every other thread can see it (but I want the variable to be set in a thread safe manner) I could simple declare it as volatile AtomicBoolean or volatile AtomicInteger and that should be good right? what are some of cases where I need to use compareAndSet? There are two important concepts in multithreading environment. atomicity visibility Volatile solves the visibility problem but it does not deal with atomicity e

CAS vs synchronized performance

别来无恙 提交于 2019-12-03 17:01:55
问题 I've had this question for quite a while now, trying to read lots of resources and understanding what is going on - but I've still failed to get a good understanding of why things are the way they are. Simply put I'm trying to test how a CAS would perform vs synchronized in contended and not environments. I've put up this JMH test: @BenchmarkMode(Mode.AverageTime) @OutputTimeUnit(TimeUnit.NANOSECONDS) @Warmup(iterations = 5, time = 5, timeUnit = TimeUnit.SECONDS) @Measurement(iterations = 5,

Fetch-and-add using OpenMP atomic operations

谁都会走 提交于 2019-12-03 13:44:25
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 this even be done? N.B., the following code almost does what I want: #pragma omp atomic x += a Almost – but not quite, since I really need the old value of x . fetch_and_add should be defined to produce the same result as the following (only non-locking): template

Why we need lock prefix before CMPXCHG [duplicate]

拥有回忆 提交于 2019-12-01 19:33:40
问题 This question already has answers here : Is x86 CMPXCHG atomic, if so why does it need LOCK? (3 answers) Closed last year . why do we need lock prefix before CMPXCHG in intel architecture. please see for reference http://courses.engr.illinois.edu/ece390/archive/spr2002/books/labmanual/inst-ref-cmpxchg.html what i am not sure what are the consequences if don,t use lock. because between loading the value into eax and exceuting LOCK CMPXCHG the value could be changed irrespective of the lock

Why we need lock prefix before CMPXCHG [duplicate]

不问归期 提交于 2019-12-01 18:26:42
This question already has an answer here: Is x86 CMPXCHG atomic, if so why does it need LOCK? 3 answers why do we need lock prefix before CMPXCHG in intel architecture. please see for reference http://courses.engr.illinois.edu/ece390/archive/spr2002/books/labmanual/inst-ref-cmpxchg.html what i am not sure what are the consequences if don,t use lock. because between loading the value into eax and exceuting LOCK CMPXCHG the value could be changed irrespective of the lock prefix because loading value into eax and LOCK CMPXCHG are two instructions. Mean to say if i dont use CMPXCHG the worst thing

MySQL Atomic UPDATE in InnoDB vs MyISAM

空扰寡人 提交于 2019-12-01 17:18:58
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 compatible with both transactional and non-transactional database tables. This is the method that is recommended for MyISAM , but I am uncertain as to whether this works for InnoDB since the documentation suggests using transactions instead. Yes. In InnoDB, the row will be locked (make you have an unique index on id, the update locks all rows it has to