Skip to main content
Filter by
Sorted by
Tagged with
5 votes
1 answer
89 views

My code is ... fragment1 // compares several regions in D1$ to D1$/D3$ __atomic_fetch_add(&lock,-1,__ATOMIC_ACQ_REL); // stmt A fragment2 // moves several regions from D1$/D3$ to D1$ ...
Henry Rich's user avatar
4 votes
0 answers
168 views

Consider this example: #include <atomic> #include <cassert> #include <thread> int main() { std::atomic<int> strong = {3}; std::atomic<int> weak = {1}; auto t1 ...
xmh0511's user avatar
  • 7,628
2 votes
0 answers
122 views

Consider this example: #include <iostream> #include <atomic> #include <thread> #include <cassert> int main(){ std::atomic<int> val = 1; std::atomic<std::atomic&...
xmh0511's user avatar
  • 7,628
0 votes
2 answers
140 views

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 <...
God_of_Thunder's user avatar
3 votes
1 answer
151 views

The ECMAScript Language Specification states: Atomics are carved in stone: Program transformations must not cause any Shared Data Block events whose [[Order]] is seq-cst to be removed from the is-...
James Page's user avatar
1 vote
2 answers
204 views

Consider this example: #include <iostream> #include <thread> #include <atomic> int main(){ std::atomic<int> val = 0; std::atomic<bool> flag = false; auto t1 = std::...
xmh0511's user avatar
  • 7,628
1 vote
1 answer
196 views

I have a bounded queue with small size that definitely fit in int. So I want to use atomic<int> instead of atomic<size_t> for indexing/counter, since int is smaller it should be faster. ...
Huy Le's user avatar
  • 1,989
0 votes
1 answer
331 views

Consider this example: // thread A: start_transaction(); update_mysql(); commit_transaction(); // remove "key" from mysql tables remove_redis_cache("key"); // thread B: std::...
xmh0511's user avatar
  • 7,628
2 votes
1 answer
146 views

I noticed std::atomic<uint64_t>{}.is_lock_free() returns true even if I switch the target platform to x86 in Visual Studio. I also checked the disassembly of an uint64_t assignment like below. ...
Tinggo's user avatar
  • 1,167
7 votes
1 answer
152 views

I want to atomically write files in Python. pathlib and tempfile should be used. I have import os from pathlib import Path import tempfile def atomic_write(f: Path, data: bytes) -> None: with ...
os_user's user avatar
  • 100
Advice
0 votes
4 replies
141 views

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 ...
Engineer999's user avatar
  • 4,159
2 votes
1 answer
90 views

Why I need to use atomic() when I have only 1 db operation inside atomic block? My AI-assistant tells me that it prevents race conditions, but I don't use select_for_update() inside. It tells that db ...
Alex's user avatar
  • 66
5 votes
0 answers
230 views

I have the following program. The relevant info is: There are 3 variables atomic<int> x,y,z accessed by all threads. 3 writer threads: Each thread read all 3 values x,y,z, and update exactly 1 ...
Huy Le's user avatar
  • 1,989
-1 votes
0 answers
223 views
+50

Looking at this implementation of multiple-producer single-consumer, which was the implementation in Rust's standard library; however, its memory order model is derived from C++. So, it should be ...
xmh0511's user avatar
  • 7,628
0 votes
1 answer
164 views

[intro.execution] p8 says: Given any two evaluations A and B, if A is sequenced before B (or, equivalently, B is sequenced after A), then the execution of A shall precede the execution of B. ...
xmh0511's user avatar
  • 7,628
3 votes
1 answer
246 views

I stumbled into an interesting issue -- when it comes to intrusive lockless stacks (single-linked lists) it seems there is a consensus on how push() should look like. All internet/AI searches (and ...
C.M.'s user avatar
  • 3,457
3 votes
1 answer
184 views

While looking through implementations of the std::atomic<T>::wait, I've found that most of them used a simple hash table for mapping the state for each atomic location. libcxx static constexpr ...
ross1573's user avatar
0 votes
1 answer
178 views

Consider this example: #include <atomic> #include <iostream> #include <chrono> #include <thread> #include <cassert> int main(){ std::atomic<int> val = {0}; ...
xmh0511's user avatar
  • 7,628
10 votes
1 answer
290 views

This is a multi-producer single-consumer implementation translated from Rust, for the language-lawyer question, rewriting it in C++ template<class T> struct Node{ std::atomic<Node*> ...
xmh0511's user avatar
  • 7,628
2 votes
0 answers
93 views

I am running simple Ping/Pong between two processes A, B with shared memory: shm_A and shm_B are in separate cache lines. Allocated with separate calls to shm_open, so probably in different pages, ...
Samuel Hapak's user avatar
  • 7,284
7 votes
1 answer
108 views

In Rust Atomics and Locks chapter 5 (available online for free), this example implementation of a one-time channel is presented: pub struct Channel<T> { pub message: UnsafeCell<...
tux3's user avatar
  • 7,431
1 vote
1 answer
189 views

Consider this outline of a simple multi-threaded application. It has one writer thread, and ten reader threads. #include <atomic> #include <thread> const int Num_readers{...
WaltK's user avatar
  • 802
6 votes
0 answers
451 views

Consider this example: #include <atomic> #include <thread> #include <cassert> int main(){ std::atomic<int> v = 0; std::atomic<bool> flag = false; std::thread ...
xmh0511's user avatar
  • 7,628
3 votes
1 answer
265 views

Consider this typical example: // Thread 1: r1 = y.load(std::memory_order_relaxed); // A x.store(r1, std::memory_order_relaxed); // B // Thread 2: r2 = x.load(std::memory_order_relaxed); // C y.store(...
xmh0511's user avatar
  • 7,628
2 votes
2 answers
271 views

Consider this example: std::atomic<bool> flag = false; int arr[2] = {}; // thread 1: arr[0] = 1; // A flag.store(true,std::memory_order::relaxed); // B // thread 2: while(!flag.load(std::...
xmh0511's user avatar
  • 7,628
5 votes
0 answers
202 views

I was checking the size of std::atomic compared to T on different platforms (Windows/MSVC, Linux/GCC, Android/Clang). For intrinsic types (like int, int64_t, etc.), the size of std::atomic matches the ...
Abhishek's user avatar
  • 251
6 votes
2 answers
321 views

#include <atomic> #include <chrono> #include <iostream> #include <thread> int main() { std::atomic<int> flag = {0}; auto t1 = std::thread([&]() { ...
xmh0511's user avatar
  • 7,628
0 votes
0 answers
92 views

I’m trying to understand how speculative execution interacts with weak memory models (ARM/Power) in the context of a spinlock implemented with a plain CAS. Example: // Spinlock acquisition attempt if (...
Delark's user avatar
  • 1,385
1 vote
1 answer
149 views

An acquire-like load... will keep everything (both stores and loads) BELOW the load/fence. But this doesn't mean that everything ABOVE/before the acquire-load will not move below... This means that ...
Delark's user avatar
  • 1,385
-2 votes
1 answer
147 views

class Sample { int a = 0; public void Run() { // main thread.Assuming this is chromium task runner. auto currentRunner = GetCurrentDefault(); somePooledRunner->PostTask( [...
breaker00's user avatar
  • 195
0 votes
0 answers
135 views

I read some QA about these two operations. But I still don't understand. acquire-release-versus-sequentially-consistent-memory-order Can I understand the difference between memory_order_acq_rel and ...
breaker00's user avatar
  • 195
2 votes
0 answers
116 views

Is it possible on any real hardware in the real world, for the updated value of an atomic integer written by one thread to become visible to another thread earlier via an indirect path, where a third ...
Qwert Yuiop's user avatar
2 votes
1 answer
131 views

I’ve spent several hours studying memory orderings, but I still have some contradictions in my head. One of them concerns the Acquire/Release memory orders. Currently, my understanding is: No ...
Eugene Usachev's user avatar
0 votes
1 answer
143 views

I was reading the implementation of Android's system property, and I am confused why is it that the barriers are used this way. I am looking at bionic/libc/system_properties/system_properties.cpp with ...
Kymdon's user avatar
  • 13
1 vote
0 answers
177 views

Since C++20, the standard library has std::atomic<uint8_t>::wait and std::atomic<uint8_t>::notify_one/all. However, these are not suitable for me, as they lack advanced features (e.g. ...
sedor's user avatar
  • 326
0 votes
1 answer
97 views

Suppose I have three threads. If x was written by thread2 and x is visible to thread1, do I have the guarantee that the latest value of x is also visible to thread3? In other words, can the new value ...
Qwert Yuiop's user avatar
2 votes
2 answers
224 views

Can the hardware reorder an atomic load followed by an atomic store, if the store is conditional on the load? It would be highly unintuitive if this could happen, because if thread1 speculatively due ...
Qwert Yuiop's user avatar
2 votes
1 answer
95 views

Updated: I already know that this is a UB for ISO C, I apologize for the vague statement I made earlier. This question originates from my previous question Can atomic operations of different sizes be ...
untitled's user avatar
  • 563
3 votes
1 answer
112 views

I try to get comfortable with Atomics in node.js. For that i created a very simple test with 2 worker threads. One that waits for a notify, and one that notfies the other. main.js const { Worker } = ...
Marc's user avatar
  • 4,049
2 votes
1 answer
205 views

For the same memory address, if I use atomic operations of different widths to operate on it (assuming the memory is aligned), for example(Assuming the hardware supports 128 bit atomic operations): #...
untitled's user avatar
  • 563
2 votes
1 answer
156 views

In order to guard a code section against repeat or concurrent execution we can use Interlocked functionality. Guarding against repeat execution is necessary for things like Dispose(), and guarding ...
DarthGizka's user avatar
  • 4,868
0 votes
2 answers
227 views

I was testing the behavior of the control dependencies in LINUX KERNEL MEMORY BARRIERS, and had a problem with the location of the fence. I was testing this on AArch64 on a Qualcomm Snapdragon 835, ...
Kymdon's user avatar
  • 13
1 vote
0 answers
112 views

I'm working on a cross-platform data structure and trying to define a compact union-based layout that allows atomic access to a 64-bit word, while also optionally accessing the lower 32-bit fields. I ...
Abhishek's user avatar
  • 251
10 votes
1 answer
903 views

During coding of std::atomic, CAS, etc, I always struggle to memorize the definition of CPP class being "TriviallyCopyable". Now I am gradually switching to C world, I accidentally found ...
PkDrew's user avatar
  • 2,301
6 votes
0 answers
247 views

In P2300, the "1.4. Asynchronous Windows socket recv" example uses a pattern to mark completion (of setting the cancellation callback) that looks like this: if (ready.load(std::...
Mircea Baja's user avatar
2 votes
1 answer
117 views

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 ...
Dang Manh Truong's user avatar
2 votes
0 answers
95 views

I've been studying several implementations of SPMC (single producer, multiple consumer) ring buffers. In many of them, I find the memory orderings to be quite conservative—often stronger than what ...
Eugene Usachev's user avatar
2 votes
1 answer
152 views

This MPSC Queue (Multi Producer Single Consumer Queue) keeps on waiting in the consumer side sometimes although I have used CAS operations. I have added CAS operation for the enqueue function. Since I ...
Dinushan Vishwajith's user avatar
5 votes
1 answer
116 views

I'm implementing a multi core system consisting of several custom/specialty CPUs. Those CPUs need to be able to support the C++11 concurrency libraries (thread/mutex etc.). I'm not sure what kind of ...
dsula's user avatar
  • 267
1 vote
1 answer
200 views

Background I'm building a cross-platform atomic abstraction layer to support 64-bit and 128-bit atomic operations for the following types: int64_t, uint64_t __int128 (on Clang platforms) A custom ...
Abhishek's user avatar
  • 251

1
2 3 4 5
81