397 questions
- Bountied 0
- Unanswered
- Frequent
- Score
- Trending
- Week
- Month
- Unanswered (my tags)
Best practices
0
votes
2
replies
101
views
Atomic concurrency: when do I need something stronger than Ordering::Relaxed?
I am building some concurrent Rust code and trying to optimize a few small pieces of shared state using atomics such as AtomicBool.
One thing I am still struggling to understand is when Rust code ...
- reputation score 56
Score of 0
0 answers
145 views
pthread_mutex_t vs std::atomic_flag speed when lock can be achieved immediately?
I'm using a multi-platform API which uses pthread_mutex_t for scoped locks, to ensure things don't do what they shouldn't be doing. I want to use the scoped lock methods provided by this API, if ...
- reputation score 371
Score of 0
1 answer
237 views
How to force evaluation of an always false function?
Have some multi-threaded code that needs to do some locking. The details of that do not matter here. But I do the canonical do { ... } while (!CAS) loop.
To keep the code concise I want to add a wait ...
- reputation score 77643
Score of 0
2 answers
169 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 <...
- reputation score 783
Advice
0
votes
4
replies
167
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 ...
- reputation score 4189
Score of 2
1 answer
139 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 ...
- reputation score 712
Score of 2
2 answers
913 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 ...
- reputation score 293
Score of 1
0 answers
126 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 ...
- reputation score 390
Score of 1
2 answers
1395 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 ...
- reputation score 928
Score of -2
1 answer
130 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(...
- reputation score 39
Score of 1
2 answers
105 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 ...
- reputation score 19
Score of 5
2 answers
181 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()...
- reputation score 63
Score of 2
1 answer
148 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 ...
- reputation score 21
Score of 5
0 answers
194 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 ...
- reputation score 1523
Score of 4
1 answer
131 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 ...
- reputation score 151