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.
50
questions
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 ...
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
...
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 ...
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 ...
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 ...
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 ...
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 ...
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,...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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:
...
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 ...
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:
...
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 ...
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 ...
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 ...
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.
...
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 ...
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 (...
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 ...
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 ...
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 ...
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 ...
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 ...
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,...
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?
...
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 ...
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 ...
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 ...
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
...
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 ...
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 ...
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 ...
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 ...
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 ...
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):
...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
-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 ...