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
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 ...
katie '-''s user avatar
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 ...
brienna's user avatar
  • 243
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 ...
Ben Hollier's user avatar
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
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 : ...
Youcef LAIDANI's user avatar
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 ...
cat's user avatar
  • 977
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). ...
noamgot's user avatar
  • 123
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? ...
Ron's user avatar
  • 31
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 ...
doplumi's user avatar
  • 263
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 ...
tigrou's user avatar
  • 421
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 ...
zam6ak's user avatar
  • 791
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: ...
user1580348's user avatar
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 ...
Viatorus's user avatar
  • 151
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 ...
Akshat Mahajan's user avatar
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 <...
M Michal's user avatar
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. ...
Joan Dimko's user avatar
4 votes
1 answer
3k views

Lock-free FIFO queue implementation

...
ShadowStar's user avatar
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 ...
Aashish Upadhyay's user avatar
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
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. ...
Yawar Murtaza's user avatar
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 ...
user5447339's user avatar
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 (...
Robert Fraser's user avatar
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 @...
TheLethalCoder's user avatar
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: ...
gstackoverflow's user avatar
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 ...
HMD's user avatar
  • 151
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. ...
Luca Fülbier's user avatar
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 ...
user1950349's user avatar
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? ...
Will's user avatar
  • 41
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 ...
aquirel's user avatar
  • 73
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. ...
tamraj_kilvish's user avatar
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? ...
yasar's user avatar
  • 1,497
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. ...
Iter Ator's user avatar
  • 271
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: ...
david's user avatar
  • 1,647
-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? ...
Damaji kalunge's user avatar
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. ...
david's user avatar
  • 1,647
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 ...
bgp2000's user avatar
  • 153
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: ...
Jacob Alley's user avatar
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 ...
mtkachenko's user avatar
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 ...
Arik's user avatar
  • 191
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 ...
Alex Zywicki's user avatar
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 ...
Isty001's user avatar
  • 53
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 ...
Justin's user avatar
  • 161
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-...
Byoungchan Lee's user avatar
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 ...
Russ's user avatar
  • 133
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: ...
user12331's user avatar
  • 137
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 ...
scame's user avatar
  • 13
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
1 answer
1k views

Generic Singleton (Thread Safe)

LifeTracker.h ...
gaurav bharadwaj's user avatar
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 ...
Demi's user avatar
  • 325
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: ...
Matt Mills's user avatar

1
3 4
5
6 7
11