Questions tagged [atomic]

An atomic operation is one which can be considered, from the perspective of the computing context in which it occurs, to be executed in a single step. Atomic operations either succeed entirely or fail, with no intermediate states.

Filter by
Sorted by
Tagged with
4 votes
3 answers
425 views

C11 zero copy lock-free triple buffer

The code is for a single producer, single consumer scenario, where the consumer only cares about the latest information shared by the producer. This is just a simple proof of concept created for linux....
mausys's user avatar
  • 49
0 votes
1 answer
125 views

C++ slim condition notifier

When writing multi-threading code, one often need the thread to wait for some condition being met. A naive approach would look like this: ...
zwhconst's user avatar
  • 137
1 vote
1 answer
167 views

Lock Guard Atomic alternative

I've recently written a Vulkan library for creating 2D applications with ease. The catch was I need std::lock_guard for my window resize event to resize resources ...
FatalSleep's user avatar
1 vote
1 answer
99 views

Implementation of a lock free queue using CompareAndSwap in Go

Here is my implementation of a lock free queue using CompareAndSwap operation. ...
Tauseef Ahmad's user avatar
3 votes
2 answers
239 views

atomic spinlock mutex class

This here is the follow-up to this question. I was recommended to implement a Lockable type (similar to std::mutex) that can work with ...
digito_evo's user avatar
8 votes
3 answers
2k views

Basic RAII spinlock class

I have written the following class that acts as a simple lock for mutual exclusion: ...
digito_evo's user avatar
0 votes
1 answer
137 views

Lock-free implementation of getAndUpdate() using atomic CAS (Compare-And-Swap) operation

We have the following class written in Kotlin Native with the new Memory Manager (which doesn't require to freeze objects): ...
Volo's user avatar
  • 101
4 votes
2 answers
1k views

Mutually exclusive execution using std::atomic?

I am currently learning more about lock free programming and wondered how I could implement mutual exclusion using std::atomics. I implemented the following code to ...
dvnlo's user avatar
  • 145
0 votes
1 answer
220 views

Flippable atomic boolean

I was trying to implement a boolean that can be atomically flipped. The suggestion on Stack Overflow is to use an integer. This would be my implementation. ...
Antonio's user avatar
  • 133
1 vote
0 answers
173 views

Test and set a bit atomically using inline aarch64 assembly

The code below has been a vehicle for learning the syntax of inline assembly for gcc/clang and that is the focus of my question. In addition to anything else that I've missed I would also like ...
Robert Harris's user avatar
3 votes
2 answers
386 views

C++11 revised `std::latch` implementation

This question follows up on this question. After turning the while-loop into a conditional wait using std::condition_variable, I ...
noes's user avatar
  • 43
1 vote
1 answer
283 views

Implementation of a latch

As an exercise to learn more about multi-threading and atomic operations work in C++, I decided to implement a latch class in C++11 loosely based off of std::latch ...
noes's user avatar
  • 43
22 votes
3 answers
6k views

C++ lock-free, MPMC Ring buffer in C++20

I have some performance critical inter-thread messaging code in C++. Multiple producers, one or more consumers. Profiling dozens of iterations of this messaging code over several years of development, ...
Ash's user avatar
  • 364
6 votes
1 answer
1k views

Implementation of std::atomic<std::shared_ptr<T>> for C++20

As you may know, C++20 has added std::atomic<std::shared_ptr<T>> specialization to the standard, but sadly, most compilers have not implemented it yet. ...
Afshin's user avatar
  • 295
1 vote
1 answer
491 views

GLSL atomic float add for architectures with out atomic float add

...
Krupip's user avatar
  • 631
3 votes
2 answers
351 views

A toy example of the concurrent stack via atomic variables and CAS pattern

I've written a toy example of the concurrent stack which has only three functions push(), peek(), and ...
user37014's user avatar
5 votes
1 answer
2k views

Count of request per second, minute in java

I want to count number of requests per second, minute. I want do it fast and thread-safe. I have 2 variables to count: ...
AnnKont's user avatar
  • 151
2 votes
1 answer
324 views

Is this C++11 seqlock implementation correct?

My code: https://ideone.com/DZeIZv ...
gavv's user avatar
  • 121
4 votes
1 answer
885 views

C++ latch implementation

Since std::latch is not in many standard C++ libraries, I tried implementing my own, is it OK from memory ordering perspective or ...
udslk's user avatar
  • 141
4 votes
2 answers
1k views

Atomic fixed-size parallel std::vector wrapper

I'm in need of a simple wrapper for a fixed-size std::vector that can ensure atomic insertion. Since my only real problem is with atomic insertion and I know ...
Exaberri Tokugawa's user avatar
1 vote
1 answer
308 views

Custom bounded memory pool allocator

Recently I put together a custom fixed size (bounded) memory pool for my job system implementation to support its internal memory management. Since it will be accessed by different threads I want to ...
Caiwan's user avatar
  • 155
4 votes
0 answers
85 views

C++14 AtomicRoundRobinPool for sharing a pool of keys

The idea here is that we have some resource that is relatively very expensive to generate, but once we have a bunch of them in a pool, we can keep reusing them instead of generating new ones. "HTTP ...
Quuxplusone's user avatar
  • 19.3k
1 vote
1 answer
155 views

A minimalistic kind of read-copy-update class

This is a very simple read-copy-update (RCU)-inspired synchronization class: ...
Ignorant's user avatar
  • 119
9 votes
3 answers
5k views

C++ lock free, single producer, single consumer queue

Assumptions for use: push() will only ever be called by a single thread. pop() will only ever be called by a single thread. Requirements: push() needs to be as fast as possible. Requirements are ...
Phi's user avatar
  • 93
1 vote
1 answer
296 views

Passing objects atomically across threads without locks or data races for audio synchronization

I am learning about one of the hardest parts of Audio development: the synchronization between the audio thread and the GUI thread. Per the discussion here https://forum.juce.com/t/timur-doumler-...
MatkatMusic's user avatar
5 votes
0 answers
900 views

A multithreaded, growable vector with immutable elements, which has wait-free reads

Disclaimer for future readers: We experimented around with a similar approach at HASH. The linked Pull Request is working. I wrote a small vector with a few lines of unsafe Rust. The idea is to have ...
Tim Diekmann's user avatar
3 votes
1 answer
69 views

Concurrent Data Manager with minimal synchronization

So I have my own library to organize advanced critical sections at GitHub. It's based on the ResourceDataStorage class which is responsible for creating, storing and clearing resource records. I've ...
Zur13's user avatar
  • 133
2 votes
0 answers
166 views

Lock-free statically allocated async-interrupt-safe multi-consumer double buffer

I have a single threaded embedded system with nested interrupts. A writer interrupt will periodically update some global data structure with data. Reader interrupts / calls from main thread will read ...
user80551's user avatar
  • 121
11 votes
1 answer
4k views

Lock-free atomic shared pointer in C++14

I'm trying to write a lock-free implementation for atomic shared pointer. Basically, there are two class templates shared_ptr and ...
Lingxi's user avatar
  • 818
6 votes
2 answers
169 views

Simple atomic pseudo-random number generation

I like to use a single global PRNG in my c programs (similar to Javascript's Math.random) because it simplifies things and the unpredictable nature of a global is ...
Wingblade's user avatar
  • 233
1 vote
1 answer
3k views

A simple implementation of the producer/consumer pattern

Consider this class that consumes some items and handles them with the provided handler function: ...
Vahagn's user avatar
  • 211
4 votes
0 answers
89 views

Swift framework that facilitates making atomic changes to several objects at a time

For the purposes of an app I work on I have created a separate framework that abstracts away the mechanism of making atomic changes to the complex object structures. Each class that has to ...
0x416e746f6e's user avatar
2 votes
0 answers
414 views

Concurrent ObjectPool using ConcurrentLinkedQueue

Main ObjectPool Class: It uses atomic operations and a concurrent linked queue to achieve the best performance compared to Apache commons pool. I want to make sure there are no concurrency issues even ...
Rajdip Patel's user avatar
3 votes
1 answer
334 views

Double checked locking with shared_ptr<>

I have an unordered_map<int, std::shared_ptr<foo>> into which I want to lazily create the instances of foo based on ...
Nim's user avatar
  • 460
6 votes
2 answers
1k views

Atomic floating-point addition

I need to be able to atomically add a 32-bit floating point value to a location in memory. Here is what I came up with. While the code is Windows-specific, I'll extend it with Linux support using <...
François Beaune's user avatar
2 votes
1 answer
46 views

Stored procedure to swap account holders

I'm doing an exercise, and would like to know if what I'm doing is correct and follows the correct structure for rollback in case a transaction fails. I would like to know as well if my ...
KaiZ's user avatar
  • 23
3 votes
1 answer
157 views

Async safe threadpool

I'm trying the write a threadpool that can safely be added inside a signal handler or in code forked by multithreaded code. Are there any corner cases that would cause this code to fail? What could be ...
clockley1's user avatar
  • 133
12 votes
1 answer
4k views

Copyable Atomic

I find it pretty annoying that c++11 atomics can't be copied. The reasons for this have been discussed e.g. here and I don't want to argue about them now. However, I find myself repeatedly in ...
MikeMB's user avatar
  • 593
2 votes
0 answers
139 views

Controller method to add a product, stored across three database tables

I have some questions on how I can improve this "add" action method in a controller. Using a single form, the user can upload a product to be displayed on the site. A Product is composed of three ...
Ricardo's user avatar
  • 265
3 votes
2 answers
144 views

Single reader - multiple writer waitable lock-free unreliably ordered stack

The main body of code is located here. In the code linted_error is a platform specific type for error codes. As well, as an optimization the code can use Linux ...
Molly Stewart-Gallus's user avatar
5 votes
2 answers
157 views

Lock implementation using atomics

I was recently given an interview and was asked to write my own lock implementation using atomics, but not necessary reentrant. I didn't answer the question, but at home, I wrote this code. Please ...
gstackoverflow's user avatar
7 votes
1 answer
732 views

Fast non-fair semaphore

This is a simple non-fair semaphore implementation using Java atomic objects. It currently performs a little over twice as fast as java.util.concurrent.Semaphore on ...
user2296177's user avatar
  • 3,533
3 votes
3 answers
565 views

SPSC Wait Free Ring Buffer for incoming messages

This is for a single producer and single consumer wait free ring buffer. The writes need to be wait free for sure. It pre-allocates messages slots and uses a claim strategy to capture a buffer for ...
sun's user avatar
  • 33
2 votes
1 answer
333 views

Unreliable shared memory circular buffer with read-only consumer

This code is slightly platform dependent but should be pretty easy to port. The goal with this code was to create a circular buffer where the consumer could be limited to read-only access of the ...
Molly Stewart-Gallus's user avatar
3 votes
1 answer
873 views

Transaction.atomic with continuations

I have a view that looks like this: ...
Kos's user avatar
  • 213
4 votes
1 answer
89 views

Adding hostname to block list after x consecutive failures in multithreading application

I am using Callable in my code which will be called by multiple threads as shown below. Given a user id in DataKey object, I will find out what are the ...
arsenal's user avatar
  • 2,167
6 votes
1 answer
4k views

Lockless multi user, multi consumer FIFO queue using C++11

One of the features that is missing from C++11 are lockless queues. Therefore I have decided to write one myself. A few remarks: I know the code isn't technically lockless. However the lock is only ...
laurisvr's user avatar
  • 165
6 votes
1 answer
1k views

C++ concurrency library

I started a C++11 library of concurrency primitives in order to study and compare their performance; provide high-quality implementation of those to use in my projects. Its main target platform is ...
Aleksey Demakov's user avatar
5 votes
1 answer
5k views

Lock-free ringbuffer with multiple readers in C++11

Basic Info I needed a lock-free ringbuffer with multiple readers (but one writer). However, I did not want the writer to check all readers every time in order to prevent an overrun. Thus, I decided ...
Johannes's user avatar
  • 153
6 votes
1 answer
688 views

General Batched Job Runner

This question is inspired by: Generic Task Scheduler where the problem is to run tasks on a scheduled basis, in parallel, and have individual timeouts for each job. For example, consider this use-...
rolfl's user avatar
  • 97.3k