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.
4
votes
0answers
14 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 ...
0
votes
0answers
27 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, ...
6
votes
2answers
78 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 <...
2
votes
2answers
81 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.
...
3
votes
1answer
47 views
3
votes
2answers
51 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
3answers
268 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 ...
3
votes
2answers
78 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
126 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
59 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 (...
4
votes
2answers
195 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
46 views
Simplified LogService (from concurrency in practice) with shutdown feature
Brian Goetz provided following code of LogService with shutdown feature:
...
3
votes
0answers
38 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
32 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
163 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
60 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 ...
4
votes
1answer
80 views
Multi-threaded code to handle messages from a provider
Having a look at my code, is there a way to be losing elements?
...
6
votes
2answers
66 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
95 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.
...
4
votes
1answer
63 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?
...
4
votes
2answers
440 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. ...
0
votes
0answers
31 views
Safe locking, replaced thread by task inside Job Manager
I replaced the thread by a task so there's no thread locked when there is no job to execute for hours.
Now i'm not sure if the lock without an AutoResetEvent is lock safe when AllowParallelExcuteion ...
0
votes
1answer
137 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:
...
-3
votes
2answers
42 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
2answers
114 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.
...
0
votes
2answers
239 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 ...
3
votes
1answer
59 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:
...
3
votes
1answer
249 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 ...
7
votes
1answer
133 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
2answers
193 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 ...
3
votes
1answer
69 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 ...
4
votes
0answers
34 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 ...
1
vote
2answers
219 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-...
1
vote
0answers
49 views
Combination of Java's Future and Android's AsyncTask
My goal is create a class which implements Future interface of Java (get, cancel, isDone...), yet provides callback like ...
2
votes
1answer
106 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 ...
1
vote
2answers
202 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
1answer
53 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
2answers
58 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
1answer
139 views
2
votes
2answers
88 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 ...
5
votes
1answer
95 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:
...
0
votes
0answers
51 views
Threadpool with abortable jobs and then
I have written a threadpool based on boost::asio where I can file jobs and receive a handle to abort jobs. When adding a job, I can (must, at the moment) provide a "...
5
votes
2answers
96 views
Snoozy: A lazy reevaluation upon get
I would appreciate critique and comments in regards of thread-safety on a basic type I'm trying to create.
In C#, the Lazy<T> type is initialized only once, ...
3
votes
0answers
186 views
C++11 generator implementation emulating Python's yield keyword
C++11's range-based loops allow convenient and easy iteration over containers, but what about more complicated iterations such as tree traversal? Usually this involves a lot of boilerplate code and ...
4
votes
1answer
68 views
Asynchronous task execution using actor based concurrency
I have a program where I need to implement asynchronous tasks (writing a directory to a file following any change to the directory). There is existing documentation within my organization for ...
6
votes
5answers
970 views
A buffer that holds and provides the latest n items
I have developed the following code to hold n latest items it has received, and when asked for provide these n latest items.
The interface is:
...
3
votes
2answers
119 views
Make all additional threads return from a method using Interlocked.Increment
I need to ensure a method is executed only by one thread at a time. Contrary to what most of the synchronization primitives do, I don't need other threads to wait, I want them to return from the ...
6
votes
2answers
656 views
Simple dispatcher implementation
I need a simple dispatcher implementation(invoke methods on one thread from many threads)
once you dispatch method curent thread should wait for results, so I'm thinking about something like this:
<...
0
votes
1answer
52 views
How to avoid carrying around a lock with my variable when multiple threads instantiation in Python?
I created an application using multiple threads defined in multiple class files which read and write into a "shared variable" (a dictionary).
To ensure thread-safety, I am using a Lock, and I pass it ...
2
votes
2answers
530 views
Thread synchronization wait/notify
I need to run several identical threads. All threads must wait to do their job, until all threads are running (alive).
I use wait/notify to synchronize: when a thread is created it waits until it ...