compare-and-swap

Should 64bit Compare&Swap (CAS) work on a 32bit machine? (or 64bit machine?)

余生颓废 提交于 2019-12-07 15:40:37
So I read that in a 32bit machine, one can use the CAS operation with aligned 64bit blocks. Similarly, in a 64bit machine, one can use the CAS operation with aligned 128bit blocks. I'm using a 32bit machine so I tried the following: // sizeof(long long) is 8 bytes, so 64 bits long long y = 12; long long z = 12; long long x = 99; __sync_bool_compare_and_swap(&y, z, x); and the CAS succeeded changing the value of y to 99 . But then I tried using a char array[8]; (which size is 64 bits) instead of a long long . And I do: char full[8] = {'0', '1', '2', '3', '4', '5', '6', '7'}; char full2[8] = {'0

Is it appropriate to use AtomicReference.compareAndSet to set a reference to the results of a database call?

走远了吗. 提交于 2019-12-06 06:33:57
问题 I am implementing a simple cache with the cache stored as an AtomicReference. private AtomicReference<Map<String, String>> cacheData; The cache object should be populated (lazily) from a database table. I provide a method to return the cache data to a caller, but if the data is null (ie. not loaded), then the code needs to load the data from the database. To avoid synchronized I thought of using the compareAndSet() method: public Object getCacheData() { cacheData.compareAndSet(null,

atomic compare(not equal) and swap

爷,独闯天下 提交于 2019-12-06 02:53:44
问题 I want to use atomic compare and swap, but instead of equal to, I want to swap only if the memory location is not equal to the old value. Is it possible in C? 回答1: How about this: void compare_and_swap_if_not_equal(word_t const required_non_value, word_t const new_value, word_t* ptr_to_shared_variable) { for (;;) { word_t const snapshot_value = *ptr_to_shared_variable; if (required_non_value == snapshot_value) { break; // or (sleep and) 'continue;', if you want to wait for the stored value to

Java Atomic Variable set() vs compareAndSet()

。_饼干妹妹 提交于 2019-12-05 22:55:42
问题 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? 回答1: The set and compareAndSet methods

Can anyone interpret this C++ code (from OpenJDK6) into plain English?

拈花ヽ惹草 提交于 2019-12-05 16:51:12
Here's a code snippet from OpenJDK6's hotspot/src/share/vm/prims/unsafe.cpp (starting on line 1082): // JSR166 ------------------------------------------------------------------ UNSAFE_ENTRY(jboolean, Unsafe_CompareAndSwapObject(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jobject e_h, jobject x_h)) UnsafeWrapper("Unsafe_CompareAndSwapObject"); oop x = JNIHandles::resolve(x_h); oop e = JNIHandles::resolve(e_h); oop p = JNIHandles::resolve(obj); HeapWord* addr = (HeapWord *)index_oop_from_field_offset_long(p, offset); if (UseCompressedOops) { update_barrier_set_pre((narrowOop*)addr,

Why are CAS (Atomic) operations faster than synchronized or volatile operations

£可爱£侵袭症+ 提交于 2019-12-05 14:56:20
From what I understand, synchronized keyword syncs local thread cache with main memory. volatile keyword basically always reads the variable from the main memory at every access. Of course accessing main memory is much more expensive than local thread cache so these operations are expensive. However, a CAS operation use low level hardware operations but still has to access main memory. So how is a CAS operation any faster? OldCurmudgeon I believe the critical factor is as you state - the CAS mechanisms use low-level hardware instructions that allow for the minimal cache flushing and contention

how to prevent corruption in concurrent lifo stack implemented with atomic compare and swap

天涯浪子 提交于 2019-12-05 13:10:30
Below is a simplified C program that demonstrates a problem I am having with a concurrent stack implemented using the GNU built in compare and swap on an intel cpu. It took me a while to understand what was happening, but now that I do I see that it is well within the guarantees provided by atomic compare and swap. When a node is popped from the stack, modified, then placed back on the stack, the modified value may become the new head of the stack, corrupting it. The comments in test_get describe the order of events that cause this. Is there any way to reliably use the same node with the same

Why Double-Checked Locking is used at all?

拥有回忆 提交于 2019-12-05 01:53:49
问题 I keep on running across code that uses double-checked locking, and I'm still confused as to why it's used at all. I initially didn't know that double-checked locking is broken, and when I learned it, it magnified this question for me: why do people use it in the first place? Isn't compare-and-swap better? if (field == null) Interlocked.CompareExchange(ref field, newValue, null); return field; (My question applies to both C# and Java, although the code above is for C#.) Does double-checked

Fetch-and-add using OpenMP atomic operations

喜你入骨 提交于 2019-12-04 20:31:45
问题 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 .

How Compare and Swap works

依然范特西╮ 提交于 2019-12-04 10:07:54
问题 I have read quite some posts that say compare and swap guarantees atomicity, However I am still not able to get how does it. Here is general pseudo code for compare and swap: int CAS(int *ptr,int oldvalue,int newvalue) { int temp = *ptr; if(*ptr == oldvalue) *ptr = newvalue return temp; } How does this guarantee atomicity? For example, if I am using this to implement a mutex, void lock(int *mutex) { while(!CAS(mutex, 0 , 1)); } how does this prevent 2 threads from acquiring the mutex at the