programming concept applicable in the context of multi-threaded programs. A piece of code is thread-safe if it only manipulates shared data structures in a thread-safe manner, which enables safe execution by multiple threads at the same time. (Wikipedia)
0
votes
1answer
24 views
Is this code thread safe?
I do a code review for ASP.NET MVC RESTful service application code. Controller looks something like
public class BarController : ApiController
{
[HttpPost]
public void DoBar(byte[] rawData)
...
2
votes
1answer
35 views
Synchronization of remote files download
Preamble: it's a self-assigned and pure syntetic task to learn (and remember what I already knew) C# threads and synchronization and data structures.
The original question was here ...
3
votes
1answer
62 views
Dining Philosophers problem Solution with Java Reentrant Lock
I have implemented Dining Philosopher problem using ReentrantLock in java.
The goal of this program is
Every philosopher should follow the workflow of think,getchopsticks,eat,putchopsticks (No ...
1
vote
1answer
69 views
Thread safe settings
I'd like to design a settings class thread save. The settings have 2 attributes: String x and int y and should provide listener functionality to notify listener about changes. The problem is, how to ...
1
vote
1answer
31 views
Ensuring my program is thread safe
I have a class which is responsible for waiting until a message is added to a message list and then sending it off to get processed
withdrawMessages - waits for message. I wait a total of 2 minutes ...
-1
votes
0answers
38 views
What is so specific about reentrancy? [closed]
(the question was closed at Quality Assurance though I do not understand how it may be no related to that)
Developers are obsessed with ensuring (or avoiding) the reentrancy when it regards to ...
2
votes
1answer
99 views
Review of simple Java Actor library
How can I improve this code?
Also available from git://github.com/edescourtis/actor.git .
Actor.java
package com.benbria.actor;
public interface Actor<T> extends Runnable {
public ...
3
votes
2answers
55 views
Is this a scenario to use volatile instead of synchronized?
I want to know if using volatile in this scenario will give a better performance than using synchronization. Specifically for the paused and running instance variable in the SimulationManager class.
...
2
votes
0answers
277 views
Lock-free multiple-consumer multiple-producer queue
The code below implements an intrusive lock-free queue that supports multiple concurrent producers and consumers.
Some features:
Producers and consumers work on separate ends of the queue most of ...
3
votes
1answer
187 views
Java thread safety and caching techniques
This is my first Java multi-threading code. It is part of an Android application that is a client to a webpage that serves books. Each page of the book is in a separate PDF, and the Book class ...
2
votes
4answers
154 views
Designing a better logger class
Could you please critisize the logger class below? Can it be used in a multi threaded web environment? If not how can I improve it? Is there anything wrong with locking in WriteToLog method or ...
3
votes
1answer
101 views
Thread Safety issues in the multithreading code
I am working on a project in which I have two tables in a different database with different schemas. So that means I have two different connection parameters for those two tables to connect using ...
2
votes
1answer
138 views
Lock free MPMC Ring buffer implementation in C
I have written a lock free MPMC FIFO in C based on a ring buffer. It uses gcc's atomic built-ins to achieve thread safety. The queue is designed to return -1 if it's full on enqueue or empty on ...
4
votes
3answers
98 views
Is this Agent/Actor implementation issue free?
I implemented this Agent class for a project recently and was wondering if I could get some other eyes to look at it -- I'm currently the only developer where I work so I can't exactly ask someone ...
2
votes
2answers
89 views
Simple FIFO Job list in Java
For the first time I have to play with Threads in java.
Basically, my code is a simple FIFO job queue.
The main thread regularly puts jobs in the queue wich need to be executed.
The code below is a ...
2
votes
1answer
57 views
BlockingQueue Implemetation using ReentrantLock
I was writing my own implementation of BlockingQueue for practice. I am trying to avoid using the synchronized keyword for the methods. I would instead like to use ReentrantLock.
What is the best way ...
1
vote
2answers
331 views
Lock-Free Ring Implementation
I am wondering if someone can take a look at my lock-free, circular ring implementation which I use to implement a background logger.
The CircularRing pre-allocates LoggableEntity elements and stores ...
2
votes
1answer
170 views
Publisher/Consumer thread-safe lock-free queue with a single publisher/consumer
The code above is a implementation of a lock-free queue that makes the assumption that there is exactly one Consumer thread and one Producer thread. This works as intended? The memory barriers is used ...
4
votes
6answers
387 views
Strategy for avoiding threadpool starvation while performing cpu bound jobs in a queued fashion
My aim is to avoid using threadpool threads for CPU bound work, thus avoiding a situation where IIS stops responding to new requests.
Can you see any problems with the code below? Is this a ...
4
votes
1answer
182 views
Is this custom dictionary thread-safe?
I'm writing a custom dictionary which is to be used as a helper for caching. The reason I use delegates is because it must interface with generated code, so I can't change this. However, you can ...
7
votes
2answers
163 views
Synced/Atomic access
Forward
I would love any comments you have, any ideas, any flaws you can find, and any suggestions you might have regarding the code below.
If this is similar to other implementations, I would love ...
4
votes
2answers
177 views
Multithreading correctly done?
I rarely write multithreaded code, and am on shaky ground when doing so. I make mistakes. I would like a sanity check of something I am going to introduce in one of my apps.
There will be exactly ...
8
votes
2answers
819 views
Add/Remove items thread-safely in List<T>
Recently I had to lock collections (of type List<T>) when items were added or removed. Because several collections were used in given code instead of creating helper methods for each collection, ...
1
vote
0answers
104 views
Correct Use of Boost Mutex
I have an event driven system that needs the ability to turn on and off a logger. The issues is that the user could spam a button which could try to turn on the logger over and over again. The turning ...
5
votes
1answer
85 views
Is this a thread safe way to control access to a reusable resource? Is there a better way?
public static class ThreadStatic<T> where T : new()
{
[ThreadStatic]
private static T instance;
public static T Instance
{
get { return instance ?? (instance = new T()); ...
3
votes
2answers
115 views
How is this ThreadSafe?
Given the class Sequence:
//This method getNext must return a unique value nextValue
public class Sequence {
@GuardedBy("this") private int nextValue;
public synchronized int getNext() {
...
5
votes
2answers
235 views
How can I make this fast and more readable?
I made a simple library to help me doing A/B tests in a web app. The idea is simple: I have two or more page options (url) for a given page and every call to the library method should give me a url so ...
3
votes
1answer
132 views
C# Array Mutator Methods to mimic JavaScript Array Mutator Methods
Between .NET's built-in collection libraries and LINQ extension methods, I never have to use arrays. An unfortunate consequence of this is that I am probably less competent in working with this very ...
3
votes
1answer
313 views
.Net caching service - thread safety
I have a simple cache service that i am using inside my WPF prism application, i am concerned about the thread safety of it - we will be accessing this via multiple threads and using the current code ...
4
votes
2answers
407 views
thread-safe lock-free counter
I was using such code:
class Counter
{
private int i = 0;
public int Next()
{
lock (this)
{
return i++;
}
}
public void Reset()
{
...
2
votes
1answer
271 views
Mutex implementation for uniprocessor bare metal embedded OS
I wrote this recently for one of my projects, any error you guys can spot or a feature which could be implemented without eating up resources or some optimisations ? Oh and it isn't meant for Multi ...
2
votes
0answers
522 views
The correct (and safe) way to use a Timer to poll a Delegate
This is a followup to Polling loop to run in a background thread where I was initially using a single thread and Thread.Sleep() to poll a delegate. I was recommended to use a timer and the ThreadPool ...
0
votes
1answer
79 views
Thread Safe Server Querier
I wanted to make a class that connects to my server and returns a value, a url in this case. As Android 4.0 doesn't let me run network task on the main UI thread(rightly so) I have made this. Please ...
1
vote
1answer
272 views
Loading an Object from File with Type-Safety and Thread-Safe access
I'm attempting to write a bit of code that allows for easy and safe access to objects in a file. It seems to work great but I was curious if there was an easier way to do this or if Java already has ...
1
vote
1answer
984 views
Simple generic multithreaded queue
This is a simple thread safe queue that is used in a producer-consumer model.
public class ThreadedQueue<TType>
{
private Queue<TType> _queue;
private object _queueLock;
...
4
votes
1answer
296 views
is this thread safe?
I am using this code for receiving log messages from my clients I receive more than 1000 connections per minute. I want to increase my log handling. I did with java threading. I have a doubt if it ...
1
vote
4answers
728 views
Thread-safe object cache, for read, high performance in .NET
I want to realize an object cache which holds objects loaded from database, file or other slow access source, to make them available quickly with high performance. There will be a lot of accesses to ...
1
vote
1answer
356 views
Thread safe cache implementation
Please review this implementation of thread-safe chache.
public class StaticCacheImpl<T extends Entity> implements StaticCache<T>
{
private boolean set = false;
private ...
1
vote
1answer
2k views
Generic wrapper for System.Runtime.Caching.MemoryCache
I have implemented a simple generic wrapper for MemoryCache class. Its interface looks like ConcurrentDictionary class but I tried to keep it simple. I tried to keep operations atomic, hoping not to ...
2
votes
2answers
219 views
Is this method thread safe?
Are these methods getNewId() & fetchIdsInReserve() thread safe ?
public final class IdManager {
private static final int NO_OF_USERIDS_TO_KEEP_IN_RESERVE = 200;
private static final ...
4
votes
2answers
2k views
Did I need to use lock to ensure that Queue.Dequeue is Thread Safe in this case on .NET 2.0?
Is this ok? I am using C# and .NET 2.0
I have this Queue declared in my class :
static private Queue<SignerDocument> QUEUE = new Queue<SignerDocument>();
I fill this Queue with some ...
2
votes
2answers
387 views
Thread design for sending data to multiple servers?
Language: C++
Thread library: PThreads
In the following code, checkServerExists function checks if the server exists in the vector.
If it does, then the new message is directly pushed in the vector, ...
0
votes
0answers
516 views
Have I thought of everything in this wrapper around boost::thread_group?
boost::thread_group doesn't automatically clean up contained threads when they end, so I needed something like that described in this boost-users list post. However, in my opinion that code is not ...
4
votes
1answer
269 views
Activeresource-response gem
I created my first gem for Rails today. https://github.com/Fivell/activeresource-response.
This gem adds possibility to access http response object from result of activeresource call.
I don't know ...
4
votes
2answers
875 views
thread-safe stl map accessor
So after learning that stl map containers are not inherently atomic and therefore not thread-safe (check out this related stackoverflow question and usage example), I decided to create code that would ...
4
votes
2answers
828 views
Is this collection actually thread safe? Is concurrent Iterating, querying and modifying safe?
This is based on Alexey Drobyshevsky's excellent article Problems with Iteration
At Alexey's suggestion, I implemented his solution using a ReaderWriterLockSlim. Then I fleshed out a ThreadSafeList : ...
3
votes
1answer
256 views
Is this code an example of safe publication or unsafe publication?
Hi all I was wondering in this piece of code:
public class Test {
public static void main(String args[]) {
ThreadB t = new ThreadB();
t.start();
t.Grab(new Bag("HI"));
...
4
votes
1answer
1k views
Singleton class extending a parent class to utilise shared functionality
I have a singleton class which extends from an abstract java class. Two singleton classes extend from ItemImageThreadManager, the reason for this is to use shared scheduling functionality. A thread is ...
10
votes
2answers
5k views
Create thread safe singleton class
#include <boost/thread/mutex.hpp>
class Singleton
{
public:
static Singleton& GetInstance()
{
boost::mutex::scoped_lock lock(m_mutex);
static Singleton instance;
return ...
1
vote
1answer
205 views
Is this a safe/correct way to make a python LogHandler asynchronous?
I'm using some slow-ish emit() methods in Python (2.7) logging (email, http POST, etc.) and having them done synchronously in the calling thread is delaying web requests. I put together this function ...
