thread-safety

Is it possible to get an IEnumerator<T> from a T[]?

Let's say I want to create a collection class that is thread-safe by default. Internally, the class has a protected List<T> property called Values. For starters, it makes sense to have the class implement ICollection<T>. Some of this interface's members are quite easy to implement; for example, Count returns this.Values.Count. But imp...

Thread Safety on my method.

Hi, Is this code considered thread safe even though multiple threads may be polling the directory for files on the webserver at once? Thanks, Mike //Get a testimonial virtualRoot = HostingEnvironment.ApplicationVirtualPath; configuration = WebConfigurationManager.OpenWebConfiguration(virtualRoot); pathToNewsDirectory =...

Is this the right approach for a thread-safe Queue class?

Hey guys, I'm wondering if this is the right approach to writing a thread-safe queue in C++? template <class T> class Queue { public: Queue() {} void Push(T& a) { m_mutex.lock(); m_q.push_back(a); m_mutex.unlock(); } T& Pop() { m_mutex.lock(); T& temp = m_q.pop(); m_mutex.unlock(); return temp; } private...

Enumerators and Thread-safety

Just to make sure, say that i have this code: this.allObjects = [some linq query]; and have two methods that both read (not modify) this IEnumerable, are they safe to call in parallel? Just looping through a IEnumerable should be safe right? ...

How does "this" escape the constructor in Java?

I've heard about this happening in non thread-safe code due to improperly constructed objects but I really don't have the concept down, even after reading about in in Goetz's book. I'd like to solidify my understanding of this code smell as I maybe doing it and not even realize it. Please provide code in your explanation to make it stick...

Is JOptionPane.showMessageDialog thread safe?

JOptionPane.showMessageDialog is supposed to be a useful utility for getting user feedback as it blocks your current thread while you wait. I would expect therefore that it would be thread-safe and that you wouldn't need to wrap the call in an invokeLater or an invokeAndWait. Is this the case? ...

.NET DynamicMethod Thread Safe?

If I write a DynamicMethod with an ILGenerator and the code that I output is thread safe would the resulting delegate be threadsafe? My concern is that the IL gets compiled the first time the method runs. If that is true what happens if some other thread tries to run the delegate while it is compiling? ...

C#: How can I make an IEnumerable<T> thread safe?

Say I have this simple method: public IEnumerable<uint> GetNumbers() { uint n = 0; while(n < 100) yield return n++; } How would you make this thread safe? And by that I mean that you would get that enumerator once, and have multiple threads handle all the numbers without anyone getting duplicates. I suppose a lock nee...

Word Tearing on x86

Under what circumstances is it unsafe to have two different threads simultaneously writing to adjacent elements of the same array on x86? I understand that on some DS9K-like architectures with insane memory models this can cause word tearing, but on x86 single bytes are addressable. For example, in the D programming language real is a...

.Net4, Monitor.Enter(lockObject, acquiredLock)

In .Net4, Monitor.Enter(Object) is marked as obsolete : [ObsoleteAttribute("This method does not allow its caller to reliably release the lock. Please use an overload with a lockTaken argument instead.")] public static void Enter( Object obj ) And there is a new method Monitor.Enter(lockObject, acquiredLock) with this usage : bo...

Atomic Instructions and Variable Update visibility

On most common platforms (the most important being x86; I understand that some platforms have extremely difficult memory models that provide almost no guarantees useful for multithreading, but I don't care about rare counter-examples), is the following code safe? Thread 1: someVariable = doStuff(); atomicSet(stuffDoneFlag, 1); Thread...

Not thread safe Object publishing

Reading Java concurrency in practice, section 3.5: Claim is raised that public Holder holder; public void initialize() { holder = new Holder(42); } Besides the obvious thread safely hazard of creating 2 instances of Holder the book claims a possible publishing issue can occur, further more for a Holder class such as public Hold...

What is thread safe or non thread safe in PHP

I saw different binaries for php, like non thread or thread safe ? What does it mean this ? What is the difference between this packages ? ...

C#: How to create a thread-safe single instance of an IDisposable?

There is this DB-connection-like object that my web application will need. It is pretty slow to create and will be used rarely, so I would like to keep only a single instance of it around. If several requests need it at the same time, they will lock() the object and serialize their access. To make things more fun the object is an IDispo...

Java-like thread synchronization in C#

Hello, I am a Java programmer and I know a few things about threading int Java. In Java I can lock a method by using synchronized keyword: private int a; private int b; private int c; private synchronized void changeVars() { a = 4; b = 2; c = a+b; } I searched in msdn and saw that there are a few toys in c# to play with threa...

Android: Problem/bug with ThreadSafeClientConnManager downloading images

For my current application I collect images from different "event providers" in Spain. Bitmap bmp=null; HttpGet httpRequest = new HttpGet(strURL); long t = System.currentTimeMillis(); HttpResponse response = (HttpResponse) httpclient.execute(httpRequest); Log.i(TAG, "Image ["+ strURL + "] fetched in [" + (System.currentTimeMi...

Is this (Lock-Free) Queue Implementation Thread-Safe?

I am trying to create a lock-free queue implementation in Java, mainly for personal learning. The queue should be a general one, allowing any number of readers and/or writers concurrently. Would you please review it, and suggest any improvements/issues you find? Thank you. import java.util.concurrent.atomic.AtomicReference; public cl...

non-blocking thread-safe queue in C++?

Is there a thread-safe, non-blocking queue class in the C++? Probably a basic question but I haven't been doing C++ for a long time... EDIT: removed STL requirement. ...

Multiple UI threads on the same window

I don't want multiple windows, each with its own UI thread, nor events raised on a single UI thread, not background workers and notifications, none of that Invoke, BeginInvoke stuff either. I'm interested in a platform that allows multiple threads to update the same window in a safe manner. Something like first thread creates three bu...

java Vector and thread safety

I'm wondering if this code will do any trouble: I have a vector that is shared among many threads. Every time a thread has to add/remove stuff from the vector I do it under a synchronized block. However, the main thread has a call: System.out.println("the vector's size: "+ vec.size()); which isn't synchronized. Should this cause tro...