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
3 votes
2 answers
1k views

Platform independant thread pool v2

This is a continuation of this question, v3 can be found here Taking into account the advise given by Loki, an implementation of the threadpool using a ...
Tom Myles's user avatar
  • 432
24 votes
2 answers
15k views

Thread pool worker implementation

As an exercise in using C++11 features I decided to make a thread pool class. I would like to have a review on the code with focus on: Standards compliance / Portability issues / Best practices ...
Emily L.'s user avatar
  • 16.5k
10 votes
2 answers
3k views

Will this Circuit Breaker catch fire?

I was going to post this code as an answer to a recent question, but I wrote this code a little while ago (like, a year ago; if I recall correctly I wrote this after reading this article) and I'd like ...
Mathieu Guindon's user avatar
8 votes
3 answers
381 views

Having full atomicity against all the threads without impacting performance or throughputs

I am working on a project in which I construct a URL with a valid hostname (but not a blocked hostname) and then execute that URL using RestTemplate from my main ...
arsenal's user avatar
  • 2,167
5 votes
2 answers
3k views

C++20 Thread Pool

I've implemented a thread pool using C++20. I'm fairly new to concurrently/multi-threaded programming and wanted to work on a project that I could learn from while also getting to know some of the new ...
Developer Paul's user avatar
5 votes
1 answer
2k views

Linked list node-level locking

I am new to C++ threading library. I would love to get some comments on how to improve this code (if there are possible bugs, I would appreciate that feedback too). In particular, I want to refactor ...
nhtrnm's user avatar
  • 183
40 votes
1 answer
4k views

Recursive shared_mutex implementation

I found myself in need of a Readers-Writer mutex. With C++17 TR2 support not yet available in our compiler, I set out to implement std::shared_mutex so that we have ...
Emily L.'s user avatar
  • 16.5k
22 votes
5 answers
36k views

A TaskScheduler that always run tasks in a specific thread

The following is an TaskScheduler that always run tasks in a thread it maintains. When created, a name of the thread was specified. Once you schedule the first task,...
Earth Engine's user avatar
18 votes
2 answers
2k views

Thread-safe prime number source

I am working on some multi-threaded code that requires a prime-number source. The following code keeps an array of primes in memory, extending it as needed. It works in the 'int' domain, being ...
rolfl's user avatar
  • 97.3k
16 votes
4 answers
3k views

Throttling class

The idea of this class is that several threads are sending data over a network and each thread are sharing the same instance of this class and before sending N bytes over the network each thread is ...
skyde's user avatar
  • 193
16 votes
1 answer
6k views

Lock-free multiple-consumer multiple-producer queue

The code below implements an intrusive lock-free queue that supports multiple concurrent producers and consumers. Some features: Producers and consumers work on separate ends of the queue most of ...
Javier Blazquez's user avatar
11 votes
1 answer
2k views

Do not share same socket between two threads at the same time

I have a code in which I am dealing with sockets and I need to make sure that I don't share same socket between two threads. In my below code, I have a background thread which runs every 60 seconds ...
david's user avatar
  • 1,647
11 votes
1 answer
7k views

Timer to poll a Delegate

This is a followup to this where I was initially using a single thread and Thread.Sleep() to poll a delegate. I was recommended to use a timer and the ...
Alain's user avatar
  • 472
10 votes
1 answer
2k views

Platform independant thread pool

v2 of this question is here and v3 is here To get a better understanding of C++11/C++14, I thought I would develop a thread pool, even if it has been done to death! The only dependency outside of ...
Tom Myles's user avatar
  • 432
10 votes
2 answers
30k views

Synchronous and asynchronous behavior for client

I am working on a project in which I am supposed to make synchronous and asynchronous behavior of my client. The customer will call our client with a userId and we ...
david's user avatar
  • 1,647
10 votes
1 answer
53k views

Writing to file in a thread safe manner

I'm writing Stringbuilder to file asynchronously. This code takes control of a file, writes a stream to it and releases it. It deals with requests from asynchronous operations, which may come in at ...
Nathan Cooper'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
8 votes
2 answers
3k views

Portable periodic/one-shot timer thread - follow-up

This is a much improved version (I hope) of code in this previous code review of mine. I have: renamed several things. Are the names good? made it lazily start the worker thread only after the first ...
doug65536's user avatar
  • 513
8 votes
2 answers
4k views

Synchronized implementation of a bank account in Java

I am trying to implement a bank account with Java in a thread safe way. My code looks like: ...
Karan Khanna's user avatar
8 votes
2 answers
3k views

Wrapping IMemoryCache with SemaphoreSlim

I need simple caching for one of my web-services so I use the Microsoft.Extensions.Caching.Memory package. I wrapped it with ...
t3chb0t's user avatar
  • 44.2k
7 votes
3 answers
1k views

Lock-free multi-producer / multi-consumer queue in C++

I've been working on a lockless multi-producer, multi-consumer queue in an effort to learn as much as I can about concurrency, without the use of mutual exclusion. The queue uses a bounded ring buffer ...
Primrose's user avatar
7 votes
1 answer
202 views

My EventBus system

I decided to roll out my own EventBus system which is intended to be thread-safe. Hence a review should focus extra on thread safety apart from all regular concerns. The ...
skiwi's user avatar
  • 10.6k
7 votes
1 answer
9k views

Concurrent LRU cache using sychronizedMap() or ReadWriteLock

Trying to implement a simple, thread-safe LRU cache that's meant for "read mostly" use. Collections.sychronizedMap() Clean, simple, not much else to say. ...
raffian's user avatar
  • 173
7 votes
1 answer
393 views

Threadsafe oneshot-event which fires on subscription if the event was fired in the past

Assumptions Basically, a Connection class has a "Disconnect" event. Subscribing to this event isn't thread-safe, because the disconnection may fire from another thread right before I subscribe. So ...
Oachkatzl's user avatar
  • 173
7 votes
1 answer
1k views

Lock Using “Interlocked” vs lock Statement (“Monitor”) (Followup)

This is a followup to a previous question. Considering: Interlocked.CompareExchange generates a memory barrier, Intended to be used with very fast operations (...
Kaveh Shahbazian's user avatar
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 ...
Oersted's user avatar
  • 299
7 votes
4 answers
3k views

Thread-safe, Convenient and Performant Random Number Generator

I need to generate a lot of pseudo-random numbers in my software. I'm trying to create a elegant (syntax-wise) and performant class that would allow me to generate such numbers and perform other ...
Trauer's user avatar
  • 801
7 votes
4 answers
464 views

Handling messages from a server

I've never really done anything with concurrency, so I'm not sure if this is done correctly. Basically, I have a class that handles all messages from the server and decides what to do them. It uses ...
Austin's user avatar
  • 211
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 ...
andylvua's user avatar
  • 215
6 votes
1 answer
150 views

C# asynchronous notification vector

The purpose of this class is to efficiently notify another thread when data is available with minimal blocking to access/pass the data. The data reader/producer thread processes data in bulk which ...
sfanjoy's user avatar
  • 233
6 votes
1 answer
2k views

C++11 lock free collection similar to std::forward_list - follow-up

This question has been superseded by C++11 lock free collection similar to std::forward_list - follow-up 2 Thread safe and lock free collections are very hard to write, so I'd appreciate any feedback,...
Brent's user avatar
  • 451
6 votes
3 answers
191 views

Thread-safe retirement account class

Are these classes properly guarded for thread safety? Do you see any issues with any of the classes? ...
user2066049's user avatar
6 votes
2 answers
7k views

Lock Using "Interlocked" vs lock Statement ("Monitor")

A followup question can be found here, based on this question and it's answer. Considering: Interlocked.CompareExchange generates a memory barrier, Intended to be ...
Kaveh Shahbazian's user avatar
5 votes
1 answer
983 views

Multithreaded Producer-Consumer model implementation

Description I am trying to implement a robust, multi-threaded producer-consumer model with a single producer in the main thread and several consumers in other threads. The generator thread loops over ...
DocDriven's user avatar
  • 161
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 ...
Developer Paul's user avatar
5 votes
1 answer
148 views

Double LinkedList Deep Copy in Kotlin with Generics and Thread Safety

Goal Return a deep copy of a double LinkedList. Each node also contains an additional random pointer, potentially to any node or null. Code to start ...
AdamHurwitz's user avatar
4 votes
0 answers
149 views

"Lock-free" one-shot event manager

Giving a review to Oachkatzl and recently about some other synchronization problems (Lazy with invalidate and some node-locking list), I wanted to test my skills and redesign it in most light-weight ...
user avatar
4 votes
1 answer
942 views

Making a call to a server running a RESTful service

I am working on a project in which I am making a call to one of my servers using RestTemplate which is running a RESTful service and getting the response back from ...
david's user avatar
  • 1,647
4 votes
0 answers
117 views

Toggle any application feature ON or OFF - follow-up (v2)

This is the second version of my FeatureToggle service (see previous question). It still has the same purpose which is to help control whether a feature is ...
t3chb0t's user avatar
  • 44.2k
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
3 votes
1 answer
276 views

Managing socket pool per datacenter

I have a list of sockets per datacenter (in general, I have around three datacenter and each datacenter having 20 sockets, so in total I have around 60 sockets). Now I have 20 business threads which ...
david's user avatar
  • 1,647
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): ...
Oersted's user avatar
  • 299
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 ...
Developer Paul's user avatar
2 votes
1 answer
7k views

Send data on a socket from multiple threads

I have around 60 sockets, 20 business threads and 1 pinger timer thread and I want to make sure each of those thread works on different socket every time so I don't want to share same socket between ...
david's user avatar
  • 1,647
2 votes
1 answer
418 views

C++11 lock free collection similar to std::forward_list - follow-up 2

Thread safe and lock free collections are very hard to write, so I'd appreciate any feedback, especially regarding bugs. The code below is a self contained hpp, followed by some tests. This question ...
Brent's user avatar
  • 451
2 votes
1 answer
648 views

Simple multithreading C project

I'm fairly new to C, and this is my first time using pthread. I am pretty sure I implemented it correctly, however I'd like to verify it is indeed executing the ...
Richard Robinson's user avatar
1 vote
2 answers
133 views

Threadsafe filtering queue

I have implemented a thread safe filtering queue. The queue allows any objects, of the specified type to be added. A thread interested to take an object must specify which object it is interested in ...
Bruno Costa's user avatar
  • 5,566
1 vote
2 answers
906 views

Querying a list of jobs on a server using EJBs

Inspired by this question, I have a Java Enterprise application running in Glassfish 2.1 and a Java SE client that communicates with the server application. For this I have a bean that maintains a ...
Martin Schröder's user avatar
1 vote
0 answers
71 views

C# asynchronous notification vector v2

As a result of my previous post, I have implemented / refactored my code and have started making successful use of my class. However I have come across a couple of new questions: The observer must ...
sfanjoy's user avatar
  • 233
-1 votes
1 answer
840 views

Thread-safe queue mechanism

Is it my queue mechanism thread safe? I just wonder if I need concurrent collections. Need I lock Enqueue method? Console displays queue count in incorrect order, Does it affect on maxQueueCount at ...
dMilan's user avatar
  • 155