The Stack Overflow podcast is back! Listen to an interview with our new CEO.

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
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? ...
4
votes
2answers
26k 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
0answers
81 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 ...
1
vote
1answer
983 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
2answers
43 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
660 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. ...
2
votes
2answers
2k 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
1answer
210 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
1answer
2k 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
650 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
837 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
1answer
375 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 ...
5
votes
0answers
121 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
3k 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
354 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
685 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
3k 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
68 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
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
vote
1answer
713 views

Generic Singleton (Thread Safe)

LifeTracker.h ...
2
votes
2answers
203 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
1answer
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: ...
5
votes
2answers
183 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
946 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
93 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
3k 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: ...
2
votes
2answers
291 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 ...
10
votes
2answers
5k 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
68 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
14k 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 ...
2
votes
2answers
738 views

Concurrent bidirectional dictionary

I've been agonizing over this for an hour or two. I require a bidirectional dictionary that was thread safe. From what I understand about dictionaries, they're thread safe when being read from, but no ...
0
votes
2answers
2k views

Java synchronization task

I have the following task: Implement a simple command line Java application for booking bus seats. For simplicity suppose there is only one bus, initially all seats (lets say 50) are free. ...
6
votes
4answers
709 views

Exclusive access to objects

Take a look at following code: ...
5
votes
1answer
277 views

Simple Background Thread Worker Class (follow-up)

This is a follow up of Simple, generic background thread worker class What changed? Moved from synchronized lazily evaluated static instances to true Singletons per the on-demand holder idiom https:/...
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 ...
5
votes
1answer
672 views

Server connection handler in Python

I am working on a server for a smart house. This part of code is responsible for communication with client (separate application), sending requested data and ensuring everything is safe. I've decided ...
1
vote
1answer
1k views

Pipeline with std::thread vectors and queue

here is my code, it works but after few iterations it slows down and stop without any error, I'm looking for a more efficient solution. ...
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 ...
2
votes
1answer
172 views

Thread-safe Bloom Filter in Java

I have tried to implement a Bloom Filter in Java here. https://github.com/srirammanoj/skynet/tree/master/bloomfilter I just wanted to know if my implementation can be called 'thread-safe' , and if ...
1
vote
0answers
29 views

Passive FTP port assignment synchronization

I've created the following class to manage the assignment of port numbers to passive FTP server sockets. Notice that Semaphore here is to make the thread wait in ...
5
votes
2answers
1k views

Thread Safe Servlet

I am working on a JSP MVC web application. I am confused about Thread-Safe Servlet concept. Following is my code, please tell me is it thread safe or not. Also, tell me the reason that why it is ...
2
votes
0answers
39 views

Delay handling of registrations

I'm trying to solve the problem of a whiteboard receiving registrations before, during and after its own initialization. As each registration needs a bit of handling which can happen only after the ...
2
votes
1answer
986 views

Simple type-safe and thread-safe Rust event system

I'm creating a relatively simple type-safe and thread-safe Rust event system. It is to be used with and within an IRC library I'm making, but should work just fine for other use-cases. It needs to be ...
1
vote
1answer
865 views

Telegram - GetUpdates Process

My code fetches telegrams from a server periodically in the background. The server API is documented here. What do you think of the way I do multithreading by handling my ...
1
vote
1answer
2k views

Thread safe REST API

My assignment was to develop a REST service that will provide Airport information. This service is assumed to be used by multiple users, therefore data consistency (thread safety) and responding fast ...
2
votes
1answer
37 views

Synchronization of new data with previous data

I am working with synchronization for the first time. I wrote some code that I have to implement in my current work. I wanted to know if I am doing it right or am I making a mistake. Whenever a new ...
2
votes
2answers
209 views

Queuing e-mail notifications in a background thread

I'm new to parallel programming concepts. I'm trying to implement fire-and-forgot kind of method in my project. Somebody please evaluate the below code & let me whether this is thread safe or not. ...
0
votes
1answer
61 views

Loading message with Pthreads

I want to print a nice loading message with these three fading dots, while the main thread does some heavy IO stuff. This is why I implemented this: ...
2
votes
0answers
85 views

Blocking Memory Queue

This is a FIFO blocking memory queue in F# to be used as a component in a logging library. ...
4
votes
1answer
2k views

Simple worker class

I just wrote a basic worker class for this person https://stackoverflow.com/questions/35827459/assigning-a-new-task-to-a-thread-after-the-thread-completes-in-c and I wanted to ask if the code has ...