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.
523
questions
1
vote
2
answers
138
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 ...
5
votes
2
answers
7k
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 ...
3
votes
1
answer
1k
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
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 ...
1
vote
2
answers
920
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
1
answer
499
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 ...
2
votes
3
answers
3k
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
1
answer
2k
views
Thread safe static collection, is this code safe
A small caching utility, can you tell if it is thread safe?
...
5
votes
1
answer
701
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
0
answers
1k
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
1
answer
1k
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
1
answer
3k
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:
...
5
votes
1
answer
921
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 ...
8
votes
1
answer
9k
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 ...
7
votes
2
answers
10k
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
2
answers
13k
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
1
answer
3k
views
4
votes
2
answers
4k
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 ...
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 ...
2
votes
2
answers
151
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
3
answers
2k
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
2
answers
779
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
1
answer
230
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
2
answers
101
views
Simplified LogService (from concurrency in practice) with shutdown feature
Brian Goetz provided following code of LogService with shutdown feature:
...
3
votes
0
answers
70
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
0
answers
51
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
2
answers
5k
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 ...
3
votes
1
answer
94
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
2
answers
214
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
2
answers
2k
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
1
answer
5k
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?
...
8
votes
2
answers
50k
views
Writing a thread-safe queue in C++
I created a SafeQueue class, which stores pointers. It has two methods:
push: Adds a new pointer to the queue
next: If the queue is empty, returns nullptr. ...
1
vote
1
answer
1k
views
Thread safe singleton class to connect to Cassandra
I am working with Cassandra and using the Datastax Java driver for it. Here is my singleton class where it makes a connection to Cassandra:
...
-2
votes
2
answers
48
views
Which CountHolder implementaion is thread safe? [closed]
Is it sufficient to only synchronize the incrementCount?
Is it possible for all other threads to get the updated value all the time?
...
3
votes
2
answers
1k
views
Caching all the prepared statements in thread safe way
I have a below Singleton class where in my getStatement method, I populate a CHM by doing if check.
...
5
votes
2
answers
7k
views
Header only c++ singleton pattern implementation
I want to implement the singleton pattern in a header only c++ library. (Please refrain from sharing your opinion on the pattern itself!) This would be trivial if I could rely on the compiler ...
2
votes
1
answer
229
views
Executing an SQL command on a number of files
Is this thread safe?
I have a program that executes an SQL command on a number of files (selected by the user).
Here is where I create the threads:
...
4
votes
1
answer
3k
views
Read and write in C# buffer in parallel
I have a buffer - int array with defined length. I want to read and write values into this array in parallel. For instance, 5 readers and 3 writers with different ...
8
votes
1
answer
1k
views
Constant keys thread safe dictionary
I find that I use this pattern a lot where I have a dictionary which I would only read from and update and wouldn't add/remove keys.
In that case, using ...
1
vote
2
answers
1k
views
C++14 Thread Pool
I have written the following thread pool implementation in C++14.
It appears to work fine on my system, but I am looking for a second opinion on the thread safety of my implementation and any other ...
2
votes
1
answer
533
views
Basic memory pool, alignment, thread safety
Mainly for practicing purposes, I'm trying to implement a simple, yet efficient memory pool. It's basically a linked list that is able to grow, with fixed sized members. Each node has a flag if it's ...
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 ...
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-...
2
votes
1
answer
1k
views
Thread safe Pooled Dictionary
I have an object that is fairly expensive to create and has a tendency to get created multiple times because each distinct user may have one or many instances of this object. This object can expire as ...
2
votes
2
answers
5k
views
Blocking and non-blocking queue
I came up with the following for a blocking and non-blocking queue implementation. Please suggest any changes/improvements/gotchas.
Blocking queue:
...
1
vote
1
answer
75
views
Synchronize different tasks (I/O listening daemon, prompt scheduler, output etc)
Shortly, I need to create a Java application with client/server architecture (clients represent some sort of math functions and when they are done computing, server consumes these values and produces ...
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
1
answer
1k
views
Generic Singleton (Thread Safe)
LifeTracker.h
...
2
votes
2
answers
299
views
Thread-efficient nonce generations
I need to create unique nonces for cryptographic purposes in a library that I am writing. If the nonces ever fail to be unique, the consequences may be up to and including remote execution of ...
6
votes
1
answer
1k
views
Single-threaded timer
I've created a System.Threading.Timer wrapper in C#. The tasks to be triggered when the timer elapses have highly variable execution times. My design criteria are:
...