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
7
votes
0answers
775 views

C++11 Threadpool

I developed a threadpool using the standard c++11 features and was looking for feedback! Right now I think the implementation is pretty solid, but as I recently learned about multithreading I'm not ...
1
vote
0answers
190 views

Singleton lock for a queue

This is a singleton class which can be accessed anywhere including the shouldLock variable. I want to make sure that the variable is thread safe. Basically the <...
0
votes
1answer
312 views

Singleton design pattern study

I am learning design patterns and the singleton is one of them. What are your opinions on the code? Please feel free to share. I know singletons are not popular because of complications in testing. I ...
5
votes
1answer
1k views

Dining Philosopher correctness

How should I improve my solution for Dining Philosopher's problem? ...
3
votes
1answer
2k views

Thread safe tbb::concurrent_hash_map and std::set

I have a basic websocket server implemented in C++. I'm using Intel TBB's concurrent hash_map to store all the users, uniquely identified by the id. The concurrent ...
1
vote
1answer
917 views

Yet Another multithreaded FIFO queue written in C++17

Here, I have written a simple FIFO multithreaded queue with following features (if I have implemented correctly): Thread safe Supports multiple container types (given that they provide some function) ...
2
votes
0answers
596 views

Thread-safe rand for C

I have this very short program for dealing with thread-safety of C's rand. Is it a correct implementation? ...
1
vote
0answers
121 views

Thread safe scene graph

Below is my implementation of a thread safe scene graph; I'd love some feedback, especially regarding safety and efficiency. The key pieces are deferring node deletion until a single threaded portion ...
4
votes
1answer
320 views

ThreadPool implementation in C++

I have written a simple thread-pool implementation in C++ and want your comments to improve my understanding and improve my implementation. I understand I am repeating thread-pool implementation, but ...
1
vote
3answers
751 views

Simple counting fair semaphore implementation

Here is my attempt to implement a simple counting semaphore in Java. Please share your comments. ...
-1
votes
1answer
72 views

Some kind of multiple singleton

I need several singletons and I wanted them to be easily accessible via auto-complete, so I made this thing. ...
5
votes
2answers
348 views

Thread-safe BitArray

Notes: supports 64-bit platforms only ulong used where negative values make no sense library functions are assumed to be correct (whether they actually do what I ...
2
votes
1answer
1k views

Stopping a thread that runs an action periodically

I have a class that (among others) needs to run an action at fixed intervals of time. So I created a thread for this: ...
8
votes
1answer
396 views

Dynamically extensible threadpool implementation in C

Purpose For my recent project, I needed a thread pool. I saw a lot of implementations at GitHub, but most of them were quite complicated. With complication comes performance penalty. Most importantly,...
6
votes
1answer
984 views

Thread safe ConcurrentDictionary wrapper

I needed some features that ConcurrentDictionary doesn't have (such as a maximum number of items) so I created a wrapper around it. This is my first time working ...
3
votes
2answers
2k views

C++ minimal threadsafe array based on std::deque

Here is a minimal example of a threadsafe array I want to build on for a timeseries application, with the following characteristics: Ever-growing, and the already contained elements remain constant (...
1
vote
2answers
72 views

Testing an unsafe counter

I made this studying my Java book about concurrency. It's just to test. I feel it works ok but I'm not so sure about my Java. I worry maybe my code style is strange. I really didn't want that ...
4
votes
2answers
4k views

Counter GUI in Swing, updating the count in the background

I made a simple counter GUI for practice. The user presses a button to start the counter. The button click executes a SwingWorker task that updates a JLabel with ...
2
votes
1answer
303 views

Thread-safe Shared Pointer implementation in C

I've been working on a C project where I needed a small, thread-safe shared pointer, and so I wrote this implementation. It's primary use in the project is as a way of allowing multiple threads to ...
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 ...
1
vote
0answers
76 views

Synchronous cinema hall, with StampedLock

I'm beginner in Java concurrency mechanism. I wrote some application to train a synchronization between many threads. I noticed that I wrote a lot of if statements, and it looks really ugly. Maby is ...
1
vote
2answers
249 views

Execute a list of Threads in parallel

I have a list of Script that i want to execute them in parallel, i already find this way in Creating dynamic number of threads concurrently, so i use this piece of code here : ...
4
votes
1answer
394 views

AntiCSRF: simple CSRF token management for server APIs

I've been working on a full stack web application for a school project, and I wrote antiCSRF without prior experience or example code to prevent Cross-Site Request Forgery attacks, and to ...
1
vote
3answers
1k views

Notifying consumers when a producer is done

I am writing a simple program with a producer and a few consumers: the producer pushes to a queue some integers, and the consumers pop elements from the queue and print them (order doesn't matter). ...
3
votes
1answer
1k views

Thread safe static collection, is this code safe

A small caching utility, can you tell if it is thread safe? ...
4
votes
1answer
393 views

Exercise – Synchronization between threads using `pthread_mutex_t` and `pthread_cond_t`

I have got an assignment for an OS course that consists in the use of mutex and condition variables to synchronize N threads, each involved in the search of a character in a row of a NxN matrix. The ...
3
votes
0answers
991 views

Create PDF preview

I have written the following GetPDFPreview() method. It open a PDF file, create a thumbnail (using PdfDocument class) and ...
1
vote
1answer
179 views

Polling an API endpoint with timeout using channels

I have written a simple API client for my app in Go. The client is very simple, it wraps http.Client and has some convenience methods. The one method I am trying to get some feedback on, does ...
3
votes
1answer
2k views

Sending a string from anonymous thread to UI with PostMessage

I am sending a string from an anonymous thread to the UI with PostMessage in the following code: ...
4
votes
1answer
532 views

Recursive shared mutex

I've been looking for an implementation of a recursive shared mutex to handle a very special data tree inside a heavy multi-threaded application. Because boost and ...
7
votes
1answer
2k views

Implementing a thread-safe LRUCache

Here is the problem I've been trying to tackle: Design a thread-safe image caching server that can keep in memory only the ten most recently used images. I chose to implement an LRU cache to solve ...
1
vote
0answers
41 views

How to ensure there is one Executor

I am using Socket.IO-java on the desktop application to handle realtime communication with the server. I was wondering if I could get some feed back on the setup as I am not happy with how it's done, ...
7
votes
2answers
8k views

ASP.NET Core singleton acting as an HttpClient provider

I have created an ASP.NET Core singleton service class to act as an HttpClient provider after reading that reusing an instance of HttpClient is encouraged. However, I also want to be sure that the <...
3
votes
2answers
7k views

Singleton implementation of a database connection

I have implemented as follows, a class applying singleton pattern to get a global single access to database. I intend to provide a thread-safe implementation. ...
4
votes
1answer
2k views

Lock-free FIFO queue implementation

...
4
votes
2answers
2k views

Static ConcurrentDictionary to maintain static objects

I am using following code to maintain some static information. The problem I see with it is that, if the information retrieved using GeKeysFromCache is modified without using lock keyword it may lead ...
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 ...
2
votes
2answers
140 views

Accessing SQL stored procedures

The following code is from the DataAccess layer that calls the SQL stored procedure and returns the result set back to the calling code. ...
6
votes
3answers
1k views

Concurrently reading a Map while a single background thread regularly modifies it

I have a class in which I am populating a map liveSocketsByDatacenter from a single background thread every 30 seconds inside ...
5
votes
2answers
436 views

Locking in MySQL stored procedure for deduplication

Multiple upstream servers are sending notifications to a load-balanced django app. If all the upstream servers are working correctly, the app will always receive duplicates of these notifications (...
1
vote
1answer
222 views

An improved single instancing library

I previously posted a question: Single instancing class and received some good feedback on it. I have implemented most of the changes suggested by @EBrown. I also implemented the suggestion by @...
1
vote
2answers
72 views

Simplified LogService (from concurrency in practice) with shutdown feature

Brian Goetz provided following code of LogService with shutdown feature: ...
3
votes
0answers
54 views

Partition numbers as polynomial, each number in a thread

I'm beginner at multithreading programming and I wanna write a piece of my code using it. I'm trying to split different numbers into multiple parts and store them as a polynomial. I wanna do this ...
2
votes
0answers
46 views

Asynchronous service that publishes a possibly infinite amount of events

I want to implement a service that fetches eyetracking events and publishes them to all subscribers of the stream. The whole thing is supposed to run asynchronously, so it doesn't block the UI thread. ...
2
votes
2answers
2k views

Running multiple producer and single consumer in a multithread environment

I have a below class in which add method will be called by multiple threads to populate messageByChannelReference concurrent ...
1
vote
0answers
121 views

Locking for accumulating counter with statistics

I have an Accumulator class that uses AtomicLong to track some counters. I have methods to calculate statistics based on those ...
3
votes
1answer
89 views

Multi-threaded code to handle messages from a provider

Having a look at my code, is there a way to be losing elements? ...
7
votes
2answers
164 views

Synchronization of transaction processing

There's a server which does the following: Receive request with transaction id Load corresponding transaction from storage. New transaction object is returned each time Process transaction Save ...
6
votes
2answers
897 views

Insert and Remove Element in Deque using threads in C++

The following code works fine for inserting and removing an element from a deque using two threads. I would appreciate any help on how to make it better, especially in terms of thread safety. ...
5
votes
1answer
3k views

Thread Safe File Operation

I am trying to make file read and update operations thread safe and prevent race conditions in Python. Am I missing out something, or would this work in production? ...