Questions tagged [thread-safety]

A piece of code is thread-safe if it only manipulates data structures in a way that allows consistent execution of this code by multiple threads.

Filter by
Sorted by
Tagged with
36 votes
1 answer
1k views

Making a generic NSMapTable replacement written in Swift thread-safe

This is a follow-up to this question. While discussing some details about the code I posted there, I came upon a problem with thread-safety. After searching and trying different things, I reached a ...
4 votes
3 answers
1k views

Unbounded, High-performance(?), Generic, Thread-Safe(?), BatchedCircularQueue

This is a data structure I wrote: It is an circular first-in-first-out queue (a ringbuffer); It has batched removal/popping - it returns an array of a fixed buffer size; It is unbounded, and ...
2 votes
2 answers
139 views

Thread safe accessing memory cache using semaphore

I have a Web API and for some requests we are sending requests to a third party API that requires request authentication. It's a simple bearer token, which I'd like to reuse across requests, because ...
2 votes
0 answers
15 views

Recording/Playback thread device in Object Pascal

I have developed an audio recording and playback base device in Free Pascal. This device is built on Free Pascal's TThread class and utilizes the low-level audio ...
3 votes
1 answer
182 views

C++ Readers-Writer Lock using Asio's Strands

The locking mechanism is implemented in the class RW_Lock. The saved_async_completion_handler structure is used in ...
0 votes
0 answers
105 views

For loop vs If statement using conditional variable

I'm extending the sync.Pool type by limiting the amount of objects that can reside outside the pool. To achieve this I'm using ...
4 votes
1 answer
633 views

Iterate through Expression Tree

I'm trying to build an iterator (enumerator) that can select specific elements in an Expression tree by traversing the tree and deferring further iteration until ...
2 votes
1 answer
100 views

C++: algorithm that uses fixed-size buffer of data that are produced in stream, faster than the algorithm speed; modified version

This is a, I hope, an improved version of this code, limiting atomic usage (following @GSliepen, @G.Sliepen advice) (NB I'm limited to, at most C++17): ...
7 votes
1 answer
1k views

C++: algorithm that uses fixed-size buffer of data that are produced in stream, faster than the algorithm speed

I try to implement this scenario with a producer/consumer multithread pattern and I'd like to optimize synchronization and memory model directive used. This code originates from a SO question and its ...
0 votes
0 answers
38 views

Changing the state of an entity in a multi threaded environment using Stored Procedures

According to my current design various processes and threads(written in .net core or framework) will call this stored procedure to change the status of various entities in a multi threaded environment:...
6 votes
1 answer
195 views

Semaphore implementation by file locking in a bash script

This implementation is inspired by the POSIX functions sem_wait() and sem_post(). It tries to get rid of busy loops in the code ...
4 votes
1 answer
492 views

Threadsafe Non-Overlapping Timer

I was working on a project recently, where I needed to process some tasks periodically, but the execution of those tasks could not overlap with one another. ...
1 vote
1 answer
126 views

Dart Non-Reentrant Async Lock

This class synchronizes access to an async resource. If retainFutureErrors is false, it will keep retrying until there is a success. Otherwise, the ...
4 votes
2 answers
1k views

Multi-threading Class in C++

I wrote a C++ class which can take a vector of functions and their respective arguments and execute them in parallel and return a vector of results from its respective functions. ...
6 votes
1 answer
385 views

Synchonizing queue with mutex

I am trying to syncronize the queue between threads using mutex. I have 2 mil random integers in input.txt file. Main thread reads integers then puts them in queue. Other threads are simultaneously ...
0 votes
0 answers
112 views

Service Locactor with accessing service safely for .Net DI

I have implemented a service locator, but I am unsure if the services (instances) obtained from it can guarantee the security of the accessed or manipulated data. In some classes where services cannot ...
1 vote
0 answers
97 views

Thread-safe key-value cache with expiration

I need my cache hold items to be accessible by key. The items should disappear after a configured time period. I do not need the lifetime of items to be exactly as configured. All I need is to make ...
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: ...
2 votes
0 answers
133 views

Java Project Reactor/WebFlux token service

I'm trying to achieve a thread-safe reactive token service. The point is that all subscribers must wait until the token is received or updated (when expired). It works, but I want a second opinion ...
6 votes
2 answers
1k views

C++ Thread Pool Implementation using Lock Free Queue

Topic: C++ Implementation of Lock-Free Queue and ThreadPool classes In a project I'm currently working on, I need the implementation of ThreadPool along with a thread-safe queue. I've tried ...
10 votes
3 answers
32k views

Correct way to delete elements from a ConcurrentDictionary with a predicate

I have written a caching wrapper class for ConcurrentDictionary. Basically it has a timer that checks if items are expired and removes them. Since ConcurrentDictionary does not have RemoveAll method ...
7 votes
2 answers
811 views

ThreadSafe std::queue

I have written a threadsafe queue and I would like you to suggest what can be improved and how to write good unit tests for this implementation. ...
7 votes
1 answer
294 views

C++ mutex-like class using std::condition_variable

On a project I'm working on, I have a need for a mutual-exclusion primitive behaving like std::mutex or std::recursive_mutex, ...
4 votes
2 answers
247 views

Multiplayer queue optimisation

I currently have code that is working but I think it can be optimised. The code waits until a player joins and adds the player to a BlockingQueue if there is not already a Player waiting then it ...
6 votes
1 answer
7k views

Timeout watchdog using a standby thread

The simple but generic timeout class to be used watching for network connections, user input, filesystem events, and is intended to have a very simple interface specific to only our use cases (i.e. no ...
2 votes
1 answer
203 views

Time-Sensitive Synchronized Transfer of Data Buffer in C# to Multiple Clients

This is a C# program that is a reference implementation, it will be used to make a C version after it is stable and bug free. The programs are supposed to simulate sensor data collection and ...
2 votes
1 answer
120 views

Thread Pool Class

I have a thread_pool class, that mimics std::thread. (I would have liked std to have a pool, but alas that is not the case.) thread_pool.h ...
2 votes
2 answers
5k views

C++ 14 thread pool executor design

With a few experience of python and golang, I tried to make (simple) thread pool executor. Tasks of the executor must be copy-...
15 votes
1 answer
1k views

C++11 Threadpool

I have developed a threadpool using the standard c++11 features and am looking for feedback. Right now I think the implementation is pretty solid, but as I recently learned about multithreading I'm ...
2 votes
0 answers
730 views

Simple multithreading and synchronization problems in MFC

I use a worker thread for some data processing in my MFC project. In the thread-controlling function, it needs to use some parameters (declared as member variables) to the data-processing. I use a sub-...
2 votes
1 answer
60 views

Force acquiring R/W lock

I am trying to build an abstraction that forces lock acquisition before data can be accessed. This seems to work, but please tell me if what I am doing horribly wrong, and what I could improve. I am ...
4 votes
1 answer
5k views

Refreshing customer list periodically in the background

I have a method RefreshCustomersInternal that refreshes customer data from a server and returns a Task. Since this process is ...
0 votes
0 answers
182 views

Design a thread-safe Hit Counter

I have designed a Hit Counter which can conveniently be used to design a Rate Limiter Original question: https://leetcode.com/problems/design-hit-counter/ To make it thread safe, I used lock statement....
4 votes
3 answers
2k views

C++ upgradable RW lock implementation

I am using VS 2017 with C++ 17 standard set on Windows OS. What I'm missing in couple of heavily threaded projects is synchronizing mechanism that can be atomically upgraded from shared to exclusive ...
1 vote
1 answer
3k views

Print odd and even numbers using two threads in Java

I was asked below question in an interview where I need to print out even and odd numbers using two threads so I came up with below code one without synchronization and other with synchronization. <...
3 votes
2 answers
194 views

Thread-safe cached compiled Regex

I have a use case where I want to keep a pre-compiled regex, cached, for performance reasons, but the regex pattern also needs to be updated infrequently. Instances of CachedRegexFilter class will be ...
7 votes
3 answers
2k views

Thread-safe memoizer

I have searched around but I was not able to find a complete implementation of the memoize pattern in a multithreaded context. It's the first time playing with thread synchronization. The code seems ...
0 votes
1 answer
341 views

Deleting entities asynchronously in C#

I have a method that takes a collection of objects, and in turn calls a method that handles a single object. My question is, do I need to handle the tasks coming from the ...
1 vote
0 answers
2k views

Implementation of Concurrent List in .NET

I would appreciate review of this implementation of the concurrent list that is, to my knowledge, currently missing in .NET Core. Requirements Thread safe Lock free Ordered Supports get and set by ...
2 votes
1 answer
2k views

C++20 Multi-queue Thread Pool with Work Stealing

This is a follow up to my previous post which also follows up on my first post regarding my thread pool implementation. I have since made some further changes and attempted to improve performance with ...
9 votes
2 answers
961 views

Multithreaded bogosort in Rust

For some quick context, I'm fairly new to Rust and decided to give myself a difficult-but-attainable task of implementing a sorting algorithm. I chose bogosort (or stupid-sort, shuffle-sort, whatever ...
4 votes
1 answer
3k views

Is my implementation of a C++ setTimeout thread correct?

I've implemented a setTimeout thread similar to the one in JavaScript (new to thread programming) In the example on this page I see use of an atomic variable ...
15 votes
2 answers
1k views

Concurrency limit map in Go

Task: per host concurrency limits for web crawler (map[string]Semaphore). I considered chan struct{} (chan bool) approach, but ...
3 votes
2 answers
1k views

C++ thread pool and thread safe queue

This is my first time writing a custom thread pool in C++. I would love some feedback on how my code looks and if I am missing best practices or have glaring/subtle issues. Thanks! ThreadPool.hpp: <...
1 vote
1 answer
72 views

Synchronization \ backup of directories and files (Python)

stackexchange members. I'm just learning to write. I set myself a task a few days ago and today I completed its implementation. The task was to create a "backupper" (I was inspired just by ...
5 votes
2 answers
1k views

C++20 Single Queue Thread Pool

This is a follow up to my previous post. I've made a number of improvements to the thread pool and corrected some bugs as well. The most up to date version of the code is available on my Github. I ...
1 vote
1 answer
177 views

Bank account concurrency problem Swift

I tried to implement a simple BankAccount class to practice providing a solution to the common Bank Account transaction data race problem. Reference to a description of such a problem can be found ...
4 votes
1 answer
206 views

Task manager object for lazy initialization

The Problem I am developing a Visual Studio extension for importing code from a server. Because of the way Visual Studio works, multiple threads could try to perform the same operation at the same ...
3 votes
1 answer
1k views

C++ thread-safe object pool

This is a modern C++ implementation of a thread-safe memory pool -- I want to make sure I am solving the critical section problem correctly (no deadlocks, starvation, bounded waiting) and I am getting ...
8 votes
2 answers
5k views

A simple thread-safe Deque in C++

I am trying to implement a thread-safe deque in C++. ThreadSafeDeque will be used by a FileLogger class. When threads call the <...

1
2 3 4 5
11