392 questions
0
votes
2
answers
121
views
compare_exchange_strong failed to update the expected value
I am trying to implement a lock-free multiple-producer-single-consumer ring buffer in C++. Here is the full definition and the test code.
#include <iostream>
#include <memory>
#include <...
2
votes
1
answer
117
views
Strange behaviour of atomicCAS when used as a mutex
I'm trying to learn CUDA programming, and recently I have been working on the lectures in this course: https://people.maths.ox.ac.uk/~gilesm/cuda/lecs/lec3.pdf, where they discussed the atomicCAS ...
0
votes
0
answers
99
views
Thread Safe money transfer using CAS & No Locks
I am trying to solve the much talked about problem of transferring money from one account to another in a thread safe manner, given that the accounts exist in memory only.
I was able to easily solve ...
-2
votes
1
answer
113
views
Race Condition with std::atomic compare_exchange_strong
I am getting into a race condition with 2 threads while using atomic CAS.
std::atomic<int> turn(0);
int free = 0;
void Work(int pid){
while(true){
if(turn.compare_exchange_strong(...
5
votes
2
answers
148
views
Is it necessary to call load() before compare_exchange_strong()?
I was learning about C++ memory sequence, and I found this code in Unreal Engine5. My question is why not call compare_exchange_strong() directly instead of load() first?
FHttpRequest* GetFreeRequest()...
5
votes
0
answers
164
views
concurrent fetch_add and compare_exchange_weak interaction
Consider the following interaction between T1 and T2.
Can it happen that T2 will miss the notification from T1 and will suspend?
In other words, can it happen that compare_exchange_weak will succeed ...
1
vote
1
answer
131
views
Is std::atomic<std::weak_ptr<>>::compare_exchange_* guaranteed to work if the underlying pointer is expired?
I have a block of code where std::atomic<std::weak_ptr<T>> doesn't behave the way I would have expected if the underlying weak pointer is expired:
std::atomic<std::weak_ptr<Widget>...
0
votes
1
answer
140
views
C++ atomics Correct memory order and thread fencing for a shared_ptr implementation
After learning std::atomic and std::memory_order I wanted to experiment with writing a thread safe shared_ptr<T> and weak_ptr<T> implementation using atomics based on Microsoft Blog: ...
-4
votes
1
answer
560
views
How to implement a SpinLock
I was trying to implement my own custom SpinLock, but the SpinLock seems to misbehave.
I have two files, main.rs and safe.rs.
The test was done in Ubuntu 22.04.3LTS and the system specs are 4GB RAM, ...
0
votes
0
answers
128
views
x86: Instruction reorder related with `cmpxchg` (without lock prefix)?
Could a memory write instruction (mov reg to memory) after cmpxchg (without lock prefix) be recordered and executed before cmpxchg for x86?
EX1:
// try_cmpxchg_local(&local.pos, &tail, ...
0
votes
1
answer
514
views
Understanding of C++'s std::atomic<T> and compare-and-swap [duplicate]
My understanding is that compare-and-swap is something supported by hardware, e.g., CMPXCHG in x86 architecture. I have the following two confusions:
Is it that C++'s atomic does not "implement&...
0
votes
1
answer
298
views
Can we use Compare-and-Swap operation on non-atomic variable?
Hi I noticed that the CAS Compare-and-Swap operations are usually operated on atomic variables, see https://en.cppreference.com/w/cpp/atomic/atomic/compare_exchange, https://docs.oracle.com/javase/8/...
0
votes
1
answer
205
views
How to determine whether the minimum number of adjacent swaps required for sorting is odd or even?
I don't know if bubble sort is optimal under the condition that only adjacent swaps are allowed? Is there a more optimal algorithm that can directly determine the parity of the minimum number of swaps ...
0
votes
1
answer
542
views
How to use atomic operations with user-space variable in BPF?
I am trying to do a CAS with __sync_val_compare_and_swap in my eBPF code. As this operation needs to be atomic I cannot use bpf_probe_read_user. With a regular kernel variable ...
0
votes
0
answers
151
views
I built a stack using CAS, is it thread-safe?
I'm using std::atomic to create a stack that utilizes CAS.
To solve the ABA problem, I used tagged pointer.
The code is shown below.
template <typename T>
union tagged_ptr {
struct
{
...
1
vote
1
answer
170
views
Is Interlock.CompareExchange atomic inside an if statement?
I am assuming that the piece of code is threadsafe,
if ((Interlocked.CompareExchange(ref semaphore, 1, 0) == 0))
{
//Do Stuff
semaphore = 0;
}
However I am wondering why:
In my mind I see this as ...
1
vote
1
answer
427
views
C++ atomics using memory_order_acquire without a matching memory_order_release
Is there any case where we'd want to load an atomic with memory_order_acquire without a corresponding store to the same atomic with memory_order_release?
For example, if I have this piece of code:
std:...
0
votes
1
answer
229
views
Is cache coherency only an issue when storing and not when loading?
I came across this code emission for x64 were "Atomic Load" is using a simple movq whereas "Atomic Store" is using xchgq.
This link explains that Atomic Load/Stores on aligned ...
0
votes
1
answer
86
views
Swapping Nodes into Ascending Order
I have a method that adds elements into a doubly linked list, and swaps elements into ascending order if the first element is greater than the second element. If I input 23, 24, 16, it skips swapping ...
0
votes
1
answer
184
views
Atomically increment and assign to another atomic
Suppose I have some global:
std::atomic_int next_free_block;
and a number of threads each with access to a
std::atomic_int child_offset;
that may be shared between threads. I would like to allocate ...
1
vote
1
answer
282
views
C++: Atomic Tagged pointer with 16-bit counter in upper bits, can't seem to increment the counter
I'm trying to implement an atomic tagged/packed pointer, for the sake of learning.
I want to use the upper 16 bits for a uint16_t counter, and the lower 3 bits for a 3-bit tag.
So far, I've managed to ...
0
votes
1
answer
376
views
How atomic is Compare and Swap against global variable in modern computer?
Assume we implement in a modern programming language like C++. Let's say we have 5 threads t1 to t5, and we also have an array of timestamps TS[5] one for each thread. We also have a global timestamp ...
3
votes
1
answer
3k
views
What is the difference between the 'Compare And Swap' and 'Compare And Set' operations?
I'm trying to understand the 'Compare And Swap' operation, briefly called CAS. I found that it has a variant called 'Compare And Set'. They work the same way but the return is different. 'Compare And ...
0
votes
1
answer
425
views
Why the Semaphore implementation uses CAS (U.compareAndSetInt) instead of wait/notify?
I've decided to implement some clasess from the concurrency package from scratch and implemented Semaphore with wait/notify. It seems very easy and intuitive to do so. When I checked the build in ...
1
vote
1
answer
807
views
Java 8 impl on Compare And Exchange (Not Compare and Set!)
In java 17, AtomicReference has the compareAndExchange method which is like compareAndSet, but instead of returning a boolean, it returns the value right before the atomic action. I need that to ...