393 questions
- Bountied 0
- Unanswered
- Frequent
- Score
- Trending
- Week
- Month
- Unanswered (my tags)
0
votes
2
answers
152
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 <...
Advice
0
votes
4
replies
147
views
Using lockless atomic operations instead of a mutex
I recently had an interview where I was asked to show how to prevent race conditions in C or C++ between two threads operating on shared data. I used a mutex as follows :
pthread_mutex_t mutex;
int ...
2
votes
1
answer
119
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 ...
2
votes
2
answers
513
views
Cross-platform Support for 128-bit Atomic Operations in Clang (Compare-And-Swap or Equivalent)
We are currently evaluating 128-bit atomic operation support across platforms and compilers, and I wanted to confirm the level of support available in Clang specifically.
Our reference point is the ...
0
votes
0
answers
106
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 ...
1
vote
2
answers
792
views
What exactly makes Compare-and-swap (CAS) loop a better choice in highly concurrent environment?
Assuming we have just 1 cpu core and for the sake of example given the following CAS loop (in Java language, took it from here), although the question is about CAS loop in general not this code in ...
-2
votes
1
answer
118
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(...
1
vote
2
answers
99
views
Why does my lock-free stack implementation fail on Windows and older Linux systems?
I'm working on a lock-free stack implementation using C++ and CAS (compare_exchange_strong/compare_exchange_weak). The implementation seems to work fine on modern Linux systems but fails on Windows ...
5
votes
2
answers
154
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()...
2
votes
1
answer
142
views
Two instances of the same struct with same field values in are not bitwise-equal
I have a code where i want to CAS atomic struct (Update) and it returns false. That happens with expected and atomic structs having same field values (tested with one thread).
I went to cpprefernce ...
5
votes
0
answers
174
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 ...
4
votes
1
answer
121
views
How does CMPXCHG affect FLAGS register?
Intel official documentation says:
The ZF flag is set if the values in the destination operand and register AL, AX, or EAX are equal; otherwise it is cleared. The CF, PF, AF, SF, and OF flags are set ...
1
vote
1
answer
141
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
0
answers
63
views
Thread-safe lock-free min where both operands can change c++
I'm doing the parallel version of Bellman-Ford algorithm in c++ using std::atomic
This is my main function executed in multiple threads
void calculateDistances(size_t start, size_t end, const Graph&...
0
votes
1
answer
148
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: ...