compare-and-swap

Compare-and-Swap over POSIX-compliant filesystem objects

浪尽此生 提交于 2019-12-01 17:05:29
There are several operations which POSIX-compliant operating systems can do atomically with filesystem objects (files and folders). Here is a list of such presumably atomic operations : rename or move file or folder create hardlink create symlink create folder create and open an empty file Is it possible to build Compare-and-Swap algorithm for manipulating a file based on these operations? Let’s suppose we have several processes which are performing concurrent read/write on a single file. A file is characterized by its revision. Let’s say the revision is added to file name, and there is a

Compare-and-Swap over POSIX-compliant filesystem objects

僤鯓⒐⒋嵵緔 提交于 2019-12-01 16:32:55
问题 There are several operations which POSIX-compliant operating systems can do atomically with filesystem objects (files and folders). Here is a list of such presumably atomic operations: rename or move file or folder create hardlink create symlink create folder create and open an empty file Is it possible to build Compare-and-Swap algorithm for manipulating a file based on these operations? Let’s suppose we have several processes which are performing concurrent read/write on a single file. A

atomic_compare_exchange with greater-than instead of equals?

北城余情 提交于 2019-12-01 15:45:49
问题 C++11 has a 'compare and exchange' operation for atomic variables. The semantics are: Atomically compares the value pointed to by obj with the value pointed to by expected , and if those are equal , replaces the former with desired (performs read-modify-write operation). Otherwise, loads the actual value pointed to by obj into *expected (performs load operation). I want to do the same, but instead of setting *obj when the values are equal, I want it to be set when one is greater-than the

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

梦想与她 提交于 2019-11-29 20:58:21
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 while loops (as AtomicInteger does internally for methods such as incrementAndGet()). The code now

Not getting expected output using cmpxchg8b for unsigned long

拥有回忆 提交于 2019-11-29 18:16:06
I am trying to write a simple compare and swap inline assembly code. Here is my code #include <stdio.h> #include <stdlib.h> #include <stdint.h> static inline unsigned long cas(volatile unsigned long* ptr, unsigned long old, unsigned long _new) { unsigned long prev=0; asm volatile("lock cmpxchg8b %0;" : "=m"(prev) : "m"(*ptr),"a"(old),"c"(_new) ); return prev; } int main() { unsigned long *a; unsigned long b=5,c; a=&b; c=cas(a,b,6); printf("%lu\n",c); return 0; } This code should ideally print 5 but it is printing 0. What is wrong in my code ?Please help. Let me start by saying "Using inline

Java compare and swap semantics and performance

人盡茶涼 提交于 2019-11-28 17:50:46
What is the semantics of compare and swap in Java? Namely, does the compare and swap method of an AtomicInteger just guarantee ordered access between different threads to the particular memory location of the atomic integer instance, or does it guarantee ordered access to all the locations in memory, i.e. it acts as if it were a volatile (a memory fence). From the docs : weakCompareAndSet atomically reads and conditionally writes a variable but does not create any happens-before orderings, so provides no guarantees with respect to previous or subsequent reads and writes of any variables other

Is synchronizing with `std::mutex` slower than with `std::atomic(memory_order_seq_cst)`?

无人久伴 提交于 2019-11-28 17:02:35
The main reason for using atomics over mutexes, is that mutexes are expensive but with the default memory model for atomics being memory_order_seq_cst , isn't this just as expensive? Question: Can concurrent a program using locks be as fast as concurrent lock-free program? If so, it may not be worth the effort unless I want to use memory_order_acq_rel for atomics. Edit: I may be missing something but lock-based cant be faster than lock-free because each lock will have to be a full memory barrier too. But with lock-free, it's possible to use techniques that are less restrictive then memory

Atomically increment two integers with CAS

我的梦境 提交于 2019-11-27 20:37:35
Apparently, it is possible to atomically increment two integers with compare-and-swap instructions. This talk claims that such an algorithm exists but it does not detail what it looks like. How can this be done? (Note, that the obvious solution of incrementing the integers one after the other is not atomic. Also, stuffing multiple integers into one machine word does not count because it would restrict the possible range.) gby Make me think of a sequence lock. Not very accurate (putting this from memory) but something along the lines of: let x,y and s be 64 bit integers. To increment: atomic s+

Java compare and swap semantics and performance

瘦欲@ 提交于 2019-11-27 10:48:12
问题 What is the semantics of compare and swap in Java? Namely, does the compare and swap method of an AtomicInteger just guarantee ordered access between different threads to the particular memory location of the atomic integer instance, or does it guarantee ordered access to all the locations in memory, i.e. it acts as if it were a volatile (a memory fence). From the docs: weakCompareAndSet atomically reads and conditionally writes a variable but does not create any happens-before orderings, so

Is synchronizing with `std::mutex` slower than with `std::atomic(memory_order_seq_cst)`?

走远了吗. 提交于 2019-11-27 10:20:04
问题 The main reason for using atomics over mutexes, is that mutexes are expensive but with the default memory model for atomics being memory_order_seq_cst , isn't this just as expensive? Question: Can concurrent a program using locks be as fast as concurrent lock-free program? If so, it may not be worth the effort unless I want to use memory_order_acq_rel for atomics. Edit: I may be missing something but lock-based cant be faster than lock-free because each lock will have to be a full memory