thread-safety

Is this code threadsafe or why am I concern with Url.Helper

Say that I have public static class M { public static string Concat(string a, string b) { return N.AddOne(a, b); } public static string Concat2(string a, string b) { return SomeThreadSafeMethod(a, b); } } public static class N { public static string AddOne(string a, string b) { retur...

Partially thread-safe dictionary

I have a class that maintains a private Dictionary instance that caches some data. The class writes to the dictionary from multiple threads using a ReaderWriterLockSlim. I want to expose the dictionary's values outside the class. What is a thread-safe way of doing that? Right now, I have the following: public ReadOnlyCollection<MyCla...

c++ threadsafe static constructor

Given: void getBlah() { static Blah* blah = new Blah(); return blah; } In a multi threaded setting, is it possible that new Blah() is called more than once? Thanks! ...

Why am I getting "Invalid Internal state" reflection exception with Castle DynamicProxy?

We added DynamicProxy to our ASP.NET web app a couple of weeks ago. The code ran fine in dev and QA, but when we pushed to production, we got the following exception (top of stack trace only): [ArgumentNullException: Invalid internal state.] System.Reflection.Emit.TypeBuilder._InternalSetMethodIL(Int32 methodHandle, Boolean isInitLoca...

Is .NET double.ToString threadsafe?

I'm seeing an issue in a production ASP.NET application that involves the following code, which is used to render the geocoordinates of a particular object: private double _longitude; private double _latitude; public string ToCsvString() { return _latitude + "," + _longitude; } Thread.CurrentThread.CurrentCulture will be set to d...

Accessing array from multiple threads

Let's say I have two arrays: int[] array1 = new int[2000000]; int[] array2 = new int[2000000]; I stick some values into the arrays and then want to add the contents of array2 to array1 like so: for(int i = 0; i < 2000000; ++i) array1[i] += array2[i]; Now, let's say I want to make processing faster on a multi-processor machine, so i...

Django(postgresql) + lighttpd. Any issues with threading and python's postgresql driver?

I'd like to deploy my Django app (which uses postgresql as database) on lighttpd using FastCGI. For postgresql i see that Django has 2 backends available 'postgresql_psycopg2' and 'postgresql'. My question is that lighttpd being a threaded server are there any issues with any of this backends? Are they thread safe? And which one of them ...

Having one ModelAmin and using either of two form classes, depending on view that is requested

Suppose I have a model Animal. Animals have a field species which can either be "Lion" or "Dolphin". The AnimalAdmin should have two views, add_lion_view() and add_dolphin_view(), which call the standard add_view() method after making sure that either species is going to be added. I used to do this by setting the an AnimalAdmin instan...

Coding/Designing a generic thread-safe limiter (i.e. limit the execution of X() to Y many times per second)

I'm planning to design a class to limit the execution of a function to given amount within the specified time, for example: Process max. 5 files in 1 second It should be thread-safe and performance hit should be minimal. How would you design such a class? I've got couple of ideas but none of them seemed right to me. Is there any ...

Does this need explicit synchronization?

I have two threads, and I want to make sure I am doing the synchronization correctly on the LinkedBlockingQueue.. Is this correct? Or is the explicit synchronization on (messageToCommsQueue) not necessary? Declaration: private LinkedBlockingQueue<BaseMessage> messagesToCommsQueue; Method one: private void startOperationModeSta...

How can I store per-thread state between calls in Perl?

Now from what I understand under Perl ithreads all data is private unless explicitly shared. I want to write a function which stores per thread state between calls. I assume that a side effect of all data being thread private by default would allow me to use a closure like this: #!/usr/bin/perl -w use strict; use threads; { # closur...

Thread-safe variables in Linux programming

I am writing a shared library that will allow linked applications to query a resource. The resource class is implemented with only static methods (see below). It also uses a global object (well scoped in an anonymous namespace). The reason for the global variable is that I do not want to expose users of the library to the internals of t...

Is it possible to group/isolate tasks in ThreadPool when using WaitHandle.WaitAll?

The scenario I am facing is as below. Because ThreadPool is 1 instance per process so my question is that would method 1 cancel tasks queued by method 2 after 3 seconds? http request comes in *method 1 gets executed first*: ThreadPool.QueueUserWorkItem x 3 WaitHandle.WaitAll for 3 seconds *method 2 gets executed after method 1...

Is it safe to allow two threads to edit different properties of the same object at the same time?

I am writing a cataloging application that parses through and extracts information from files and stores the information from each file in an object instance. In addition to the data extracted from the file the objects also have additional meta data properties (author, tags, notes, etc..) which later get stored in a separate XML file. E...

How make custom Thread Safe Generic List return the whole list in C#?

I am a threading noob and I am trying to write a custom thread safe generic list class in C# (.NET 3.5 SP1). I've read Why are thread safe collections so hard?. After reviewing the requirements of the class I think I only need to safely add to the list and return the list. The example shows pretty much all I want except it lacks the retu...

Unit Testing an Event Firing From a Thread

I'm having a problem unit testing a class which fires events when a thread starts and finishes. A cut down version of the offending source is as follows: public class ThreadRunner { private bool keepRunning; public event EventHandler Started; public event EventHandler Finished; public void StartThreadTest() { ...

How to unit test Thread Safe Generic List in C# using NUnit?

I asked a question about building custom Thread Safe Generic List now I am trying to unit test it and I absolutely have no idea how to do that. Since the lock happens inside the ThreadSafeList class I am not sure how to make the list to lock for a period of time while I am try to mimic the multiple add call. Thanks. Can_add_one_item_at_...

On multicore MacOSX, is the following c++ code threadsafe?

#define atomicAdd OSAtomicAdd32Barrier class PtrInterface: public Uncopyable { private: typedef volatile int RefCount; mutable RefCount rc; public: inline void newRef() const { atomicAdd(1, &rc); } inline void deleteRef() const { atomicAdd(-1, &rc); } }; [This is the basis of an invasive refcounted pointer; I just ...

What are some thread safe techniques for storing data about a single user's request in a servlet?

If a servlet is not thread safe, then does that mean that all objects created and referenced during the servlet lifecycle are not thread safe? Maybe I'm not quite getting this, but in Web applications you almost always want to account for data stored during the servlet lifecycle that is pertinent to a single user's request. What are some...

Is List<T> constructor thread safe?

More specifically, is List(T)(IEnumerable(T)) thread-safe if the IEnumerable used to initialize the list is modified during the construction of the new list? ...