thread-safety

Instance constructor sets a static member, is it thread safe?

I am refactoring some code and am wondering about the use of a 'lock' in the instance constructor. public class MyClass { private static Int32 counter = 0; private Int32 myCount; public MyClass() { lock(this) { counter++; myCount = counter; } } } Please confirm Inst...

Thread-safe use of a singleton's members

I have a C# singleton class that multiple classes use. Is access through Instance to the Toggle() method thread-safe? If yes, by what assumptions, rules, etc. If no, why and how can I fix it? public class MyClass { private static readonly MyClass instance = new MyClass(); public static MyClass Instance { get { retur...

What's the best way of implementing a thread-safe Dictionary in .NET?

I was able to implement a thread-safe Dictionary in C# by deriving from IDictionary and defining a private SyncRoot object: public class SafeDictionary<TKey, TValue>: IDictionary<TKey, TValue> { private readonly object syncRoot = new object(); private Dictionary<TKey, TValue> d = new Dictionary<TKey, TValue>(); public objec...

Class design: Wrapping up a datafile into a class with respect to thread-safety and testability.

I'm writing an app in C# (.net 3.5) and I have a question about class design: I'd like to create a class which accesses a file (read, write) and provides its content to the users (instanciators) of the class. The most common operation on an instance will be to retrieve a certain value from the file. The actual read and write (io) opera...

Should I always make my java-code thread-safe, or for performance-reasons do it only when needed?

If I create classes, that are used at the moment only in a single thread, should I make them thread-safe, even if I don't need that at the moment? It could be happen, that I later use this class in multiple threads, and at that time I could get race conditions and may have a hard time to find them if I didn't made the class thread-safe i...

Is this C++ implementation for an Atomic float safe?

Edit: The code here still has some bugs in it, and it could do better in the performance department, but instead of trying to fix this, for the record I took the problem over to the Intel discussion groups and got lots of great feedback, and if all goes well a polished version of Atomic float will be included in a near future release ...

How to get equivalent of printf_l on Linux?

This function exists on OS X and allows you to pass custom local to the function. setlocale is not thread-safe, and passing locale as parameter is. If there is no equivalent, any way of locale-independent printf, or printf just for doubles (%g) will be ok. ...

Does the lock(objlocker) make that object thread safe app wide? And are static members automatically thread safe?

When you lock an object is that object locked throughout the whole application? For Example, this snippet from C# 3.0 in a Nutshell Section 19.6.1 "Thread Safety and .NET Framework Types": static void AddItems( ) { for (int i = 0; i < 100; i++) lock (list) list.Add ("Item " + list.Count); string[] items; l...

Is IStorage's Compound File Implementation thread-safe?

I'm using IStorage's Compound File Implementation from C# (StgCreateDocfile). Is it safe to access one IStorage / IStream instance from multiple threads, provided I synchronized the reads and writes myself? Or are there any COM issues that might be problematic here? For example, can I safely call EnumElements to get all streams in the ...

Erlang JInterface - is OtpMBox thread-safe?

In my Java program, I create a OtpNode and a "named" OtpMBox. Whenever a message is received via this mbox, some time-consuming operation needs to be performed after which a reply message is sent back. Since this operation is time-consuming, subsequent messages sent to the mbox won't be processed immediately. So I want to use Java thre...

Resetting a field lazy-loaded with the double-check idiom

Consider the "double-check idiom for lazy initialization of instance fields": // Item 71 in Effective Java copied from this interview with Bloch. private volatile FieldType field; FieldType getField() { FieldType result = field; if (result == null) { // First check (no locking) synchronized(this) { result = f...

How to use an AppDomain to limit a static class' scope for thread-safe use?

Howdy, I have been bitten by a poorly architected solution. It is not thread safe! I have several shared classes and members in the solution, and during development all was cool... BizTalk has sunk my battle ship. We are using a custom BizTalk Adapter to call my assemblies. The Adapter is calling my code and running things in ...

SharePoint and thread safety

I'm looking for articles, forum or blog posts dealing with SharePoint and thread safety? I'm quite sure there are some special aspects regarding thread safety that have to be considered when working with the SharePoint object model. Actually I didn't find many information about this, yet. So I'm looking forward to your answers. Bye...

Proper synchronization of Java threads using wait/notifyAll?

Here is a simplified version of my application showing what I'm doing. /* in my app's main(): Runner run = new Runner(); run.dowork(); */ class Runner { private int totalWorkers = 2; private int workersDone = 0; public synchronized void workerDone() { workersDone++; notifyAll(); } public synchronized void dowork() { ...

Code Re-entrancy vs. Thread Safety

What is the difference between the concepts of "Code Re-entrancy" and "Thread Safety"? As per the link mentioned below, a piece of code can be either of them, both of them or neither of them. Reentrant and Thread safe code I was not able to understand the explaination clearly. Help would be appreciated. ...

How low do you go before something gets thread-safe by itself?

Hi all.. I've been thinking, just how deep into everything do you have to go before something is automatically thread-safe? Quick example: int dat = 0; void SetInt(int data) { dat = data; } .. Would this method be considered threadsafe? I ussually wrap all my set-methods in mutex'es, just to be sure, but everytime I do so I can'...

What are alternative ways to suspend and resume a thread?

The two methods Suspend() and Resume() are obsolete in C# .Net 2.0. What are other alternatives and any examples? ...

How to change pane text of status bar from a thread in MFC?

I have a dialog in MFC with a CStatusBar. In a separate thread, I want to change the pane text of status bar. However MFC complains with asserts? How is it done? An example code would be great. ...

Is .NET System.Net.CookieContainer thread safe?

Is the .NET class System.Net.CookieContainer thread safe? --Update: Turnkey answered-- Is there any way to ensure thread safeness to variables which are modified during asynchronous requests (ie. HttpWebRequest.CookieContainer)? Is there any attribute to highlight thread safe classes? --Update: If thread-safeness is described on MSDN th...

Treatment of Shared in GAC Included Assemblies

I know that when creating a DLL and declaring items as "Shared" (Static in C#) that they are instantiated when first called, and then that object reference lives on as the single reference. So declaring a shared string property once set can be called again to retreive the same value. And that thread safety is then a Major concern withi...