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
2answers
897 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 ...
22
votes
2answers
13k 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 ...
5
votes
1answer
272 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 ...
10
votes
2answers
2k 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 ...
7
votes
3answers
354 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 ...
37
votes
1answer
3k 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 ...
9
votes
2answers
29k 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 ...
14
votes
4answers
28k 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,...
15
votes
1answer
5k 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 ...
8
votes
2answers
2k 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 ...
11
votes
1answer
6k 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 ...
7
votes
4answers
452 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
1answer
8k 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. ...
18
votes
2answers
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 ...
15
votes
4answers
2k 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 ...
8
votes
1answer
44k 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 ...
7
votes
2answers
1k 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: ...
6
votes
1answer
846 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
3answers
182 views

Thread-safe retirement account class

Are these classes properly guarded for thread safety? Do you see any issues with any of the classes? ...
4
votes
0answers
94 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 ...
1
vote
2answers
898 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 ...
11
votes
1answer
1k 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 ...
10
votes
1answer
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 ...
7
votes
1answer
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
1answer
175 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
1answer
168 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 ...
6
votes
1answer
88 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
2answers
257 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 ...
5
votes
2answers
4k 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 ...
4
votes
1answer
73 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 ...
4
votes
0answers
69 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 ...
3
votes
1answer
255 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 ...
3
votes
1answer
886 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 ...
2
votes
1answer
192 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
1answer
3k 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
1answer
110 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
0answers
56 views

Block a partcular machine for a particular period of time interval after x failure

This is an extension to this question I am working on a library which makes Http call to my service. I have below two requirements: If my service machine doesn't respond back (there is a socket ...
1
vote
0answers
47 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
vote
2answers
106 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
votes
1answer
443 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 ...