compare-and-swap

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

五迷三道 提交于 2019-12-22 07:51:14
问题 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

atomic swap with CAS (using gcc sync builtins)

泄露秘密 提交于 2019-12-21 17:23:27
问题 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

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

非 Y 不嫁゛ 提交于 2019-12-21 05:41:08
问题 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? 回答1: There are two important concepts in multithreading

Should std::atomic<int*>::load be doing a compare-and-swap loop?

混江龙づ霸主 提交于 2019-12-19 07:58:50
问题 Summary : I had expected that std::atomic<int*>::load with std::memory_order_relaxed would be close to the performance of just loading a pointer directly, at least when the loaded value rarely changes. I saw far worse performance for the atomic load than a normal load on Visual Studio C++ 2012, so I decided to investigate. It turns out that the atomic load is implemented as a compare-and-swap loop, which I suspect is not the fastest possible implementation. Question : Is there some reason

In Java what is the performance of AtomicInteger compareAndSet() versus synchronized keyword?

本秂侑毒 提交于 2019-12-18 10:27:47
问题 I was implementing a FIFO queue of requests instances (preallocated request objects for speed) and started with using the "synchronized" keyword on the add method. The method was quite short (check if room in fixed size buffer, then add value to array). Using visualVM it appeared the thread was blocking more often than I liked ("monitor" to be precise). So I converted the code over to use AtomicInteger values for things such as keeping track of the current size, then using compareAndSet() in

Why does GCC pad this bit-field?

旧城冷巷雨未停 提交于 2019-12-18 06:13:56
问题 Program is in C using std=c99, this is on a 64-bit machine. struct epochs { volatile unsigned int epoch : 1; volatile unsigned int pulse : 1; volatile unsigned int active0 : 7; volatile unsigned int active1 : 7; volatile unsigned int counter0 : 24; volatile unsigned int counter1 : 24; }; when I check sizeof(epochs) it gives me 12. I can tell gcc not to pad it by adding __attribute((packed)); so I can work around it. However I would really like to know why 4 bytes are added to pad this 64-bit

Why isn't atomic double fully implemented

白昼怎懂夜的黑 提交于 2019-12-18 05:29:05
问题 My question is quite simple. Why isn't std::atomic<double> implemented completely? I know it has to do with atomic RMW (read-modify-write) access. But I really don't see, why this shouldn't be possible on a double . It's specified that any trivially copyable type can be used. And of course double is among them. So C++11 requires the basic operations (load, store, CAS, exchange, etc.) that you can use with any class type. However, on integers an extra set of operations is possible ( fetch_add

Can CAS fail for all threads?

非 Y 不嫁゛ 提交于 2019-12-10 18:39:04
问题 I'm reading about [ lock cmpxchg description]) https://www.felixcloutier.com/x86/CMPXCHG.html): This instruction can be used with a LOCK prefix to allow the instruction to be executed atomically. To simplify the interface to the processor’s bus, the destination operand receives a write cycle without regard to the result of the comparison. The destination operand is written back if the comparison fails; otherwise, the source operand is written into the destination. (The processor never

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

夙愿已清 提交于 2019-12-09 16:08:43
问题 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

How to use atomicCAS for multiple variables with conditionals in CUDA

杀马特。学长 韩版系。学妹 提交于 2019-12-08 11:03:28
问题 I recently encountered a simple notion in programming but i stuck when i tried to implement it in cuda. Suppose that i have thousands of elements and i want to find the closest pair between them. I use atomicMIN in global memory (suppose that we dont want to reduce) so if the distance which is calculated by each thread is smaller than the distance stored in the global variable the atomicCAS will replace it with the smaller value. For example i have the global variable float gbl_min_dist To do