Questions tagged [atomic-swap]
22 questions
49
votes
3 answers
Lock-free swap of two unique_ptr
Swapping two unique_ptrs is not guaranteed to be threadsafe.
std::unique_ptr a, b;
std::swap(a, b); // not threadsafe
Since I need atomic pointer swaps and since I like the ownership handling of unique_ptr, is there a simple way to combine them…

user1181282
- 766
- 1
- 7
- 11
19
votes
2 answers
Atomic swap in GNU C++
I want to verify that my understanding is correct. This kind of thing is tricky so I'm almost sure I am missing something. I have a program consisting of a real-time thread and a non-real-time thread. I want the non-RT thread to be able to swap a…

Steve
- 8,153
- 9
- 44
- 91
7
votes
2 answers
Possible to create AtomicReference that can be swapped atomically?
Is there any way to implement a type of reference whose value can be exchanged with another atomically?
In Java we have AtomicReference which can be swapped with a local variable but not with another AtomicReference.
You can do:
AtomicReference r1…

finnw
- 47,861
- 24
- 143
- 221
6
votes
3 answers
Is there atomic increment with the check preconditions, that the atomic value was less than the specified value?
Is in the new standard C++ atomic increment operation with the check preconditions before the incremented the value, that the atomic value was less than the specified value?
Can I do it easier and faster than the following code?
int…

Alex
- 12,578
- 15
- 99
- 195
3
votes
1 answer
GLIB: g_atomic_int_get becomes NO-OP?
In a larger piece of code, I noticed that the g_atomic_* functions in glib were not doing what I expected, so I wrote this simple example:
#include
#include "glib.h"
#include "pthread.h"
#include "stdio.h"
void *set_foo(void *ptr) {
…

laslowh
- 8,482
- 5
- 34
- 45
3
votes
2 answers
Is OSCompareAndSwap (Mac OS X) equivalent to CMPXCHG8B?
Is OSCompareAndSwap (Mac OS X) equivalent to CMPXCHG8B?

Viet
- 17,944
- 33
- 103
- 135
2
votes
1 answer
Sun compiler's equivalent of gcc's __sync_fetch_and_add? aka Oracle Studio 12.2
Does the Oracle (Sun) Studio 12.2 C/C++/Fortran compiler for Linux 64-bit have an equivalent to the __sync_fetch_and_add function that is provided in gcc? I can't seem to find the wrapper code for the intel atomics in the Sun documentation nor the…

Geoffrey Anderson
- 1,534
- 17
- 25
2
votes
1 answer
Are Win32 InterlockedIncrement and InterlockedExchange atomic across processes?
MSDN says that the interlocked functions provide a simple mechanism for synchronizing access to a variable that is shared by multiple threads.
I am not sure if they work across threads of multiple processes if the variable is in the shared memory of…

Abhishek Jain
- 9,614
- 5
- 26
- 40
2
votes
2 answers
What's the correct way to atomically exchange two unsigned 32-bit variables (ULONG)?
I found the InterlockedExchange function which allows me to exchange two signed 32-bit variables (LONG).
But, what is the correct way to atomically exchange two unsigned 32-bit variables (ULONG) under Windows?
I do not see an obvious way to do that…

heckenpenner_rot
- 73
- 1
- 4
1
vote
1 answer
atomic operation implementation
i am using atomic operation provided by SunOs in , which is
void *atomic_cas_ptr(volatile void *target, void *cmp, void *newval);
now to make is usable, i have to check whether old value returned by this function and passed by…

peeyush
- 2,841
- 3
- 24
- 43
1
vote
1 answer
atomically changing multiple rows in mySQL
I have a mySQL table of the form
entryID (PK), UserID, entryName
Each user (as defined by his userID) may create any number of entries in this table, but for each user, the entryName must be unique. I would like to allow users to modify all of their…

Mala
- 14,178
- 25
- 88
- 119
1
vote
2 answers
Swap memory pointers atomically on CUDA
I have two pointers in memory and I want to swap it atomically but atomic operation in CUDA support only int types. There is a way to do the following swap?
classA* a1 = malloc(...);
classA* a2 = malloc(...);
atomicSwap(a1,a2);

Fabio T.
- 109
- 1
- 6
1
vote
1 answer
Atom update hangs inside of Clojure watch call
I've got a situation where I watch a specific directory for filesystem changes. If a certain file in that directory is changed, I re-read it, attach some existing cached information, and store it in an atom.
The relevant code looks like
(def posts…

Inaimathi
- 13,853
- 9
- 49
- 93
1
vote
1 answer
Can this Fast Atomic Lock implementation work?
I have a large data structure that is using striping to reduce lock contention. Right now I am using system locks but 99.99% of the time, the lock is uncontested and futhermore, the amount of time holding the lock is quite miniscule. However,…

Adisak
- 6,708
- 38
- 46
1
vote
3 answers
AtomicSwap instead of AtomicCompareAndSwap?
I know that on MacOSX / PosiX systems, there is atomic-compare-and-swap for C/C++ code via g++.
However, I don't need the compare -- I just want to atomically swap two values. Is there an atomic swap operation available? [Everythign I can find is…

anon
- 41,035
- 53
- 197
- 293