Make thread-safe a normal class with ThreadLocal<T> - c#

I have a normal class designed to be accessed by a single thread and I want to make it thread-safe so many threads can use a single instance at the same time. There are some class level methods and variables which I will make static and using locks make them thread-safe. Also methods which only use local variables are safe (each thread has it's stack) by default.
My question it about properties of the old class or more generally any non-static variable. Can I simply use ThreadLocal<T> and each thread has it's own set of properties ? Surely I will use locks and other thread-safety issues inside setters (I assume getters are safe).
And is ThreadLocal<T> performance killer ?

Getters are not as safe as you think. The Java memory model gives each thread it's own view of the heap, so if you don't synchronize access to variables then threads may read stale data. Making a variable volatile will prevent stale reads and is fine for primitives, but volatile won't make access atomic.
There are a bunch of classes in the java.util.concurrent package that might help you out. Writing thread-safe code is tricky, so I'd recommend getting a good book on the subject. Brian Goetz's "Java concurrency in practice" is pretty good.

That's not really what thread locals are for. They're intended for cases where each thread will have its own data.
In your case, I would suggest changing the field type to Map<Object, Object> and using Collections.synchronizedMap to to make it thread safe.

Related

Why are static methods in .Net framework classes always thread-safe?

I have noticed the following statement in most places of .Net framework documentation.
Question: What is the secret to this? I don't think a static class is always thread-safe. My question relates to standard classes that are available in .Net framework, and not the custom classes created by developers.
Would the method 'GetString' in static class below be thread-safe just because the method is a static method?
public static class MyClass
{
static int x = 0;
static MyClass()
{
x = 23;
}
public static string GetString()
{
x++;
return x.ToString();
}
}
The framework methods you mention are not thread-safe just from the fact they are static, but because they have been specifically designed to be thread-safe. Thread-safety is often hard to achieve, but it's usually necessary for static methods, since any state they mutate is shared between threads.
The sample method you posted isn't thread-safe, because it mutates state that is shared between threads, without any synchronization mechanism.
The easiest way to use a non-threadsafe instance method without any threading problems is to have that instance only visible to one thread (just don't put a reference to it in a static or anywhere else where a thread other than that which created it is going to access it). Indeed, this happens more than 90% of the time without any special effort to do so.
The second easiest is to associate a lock object with the instance (whether by using it as the lock object, or having a fields for both it and the lock object within the same scope) and making sure all access locks correctly.
With a static method we don't have these option, because there is no such instance and any thread could potentially call either it or another static method that clashes at any time. It may not even be from code by the same author. We also can't guarantee that other code uses the lock object(s) we have in place for use with it.
So for this reason a static method that is not threadsafe is of very limited use; pretty much only applicable to private static methods used in very limited cases, with these limitations providing the guarantee that it is only called by one thread at a time.
Hence with all public static methods one would make sure they are threadsafe (or else be very clear in documenting both that they are not threadsafe, and the justification for such a strange thing to do).
In fact, you'll find that a great many instance methods documented as "not threadsafe" actually are. The reasons they're listed as "not threadsafe" are:
If the author hasn't gone to the length of confirming their thread-safety, they'd better not claim what they aren't 100% sure about.
Being wrong in this direction is safe; the worse thing that happens is that someone adds their own synchronisation to add thread-safety they didn't need to, which won't actually break anything.
Since they haven' documented the method as threadsafe they are free to change to a non-threadsafe approach in a later version.
The specific examples you are referring to are designed to be thread safe. That is they allow concurrent access without deadlocks or race conditions.
This may not be the case in all instances of all classes, as such Microsoft have chosen to explicitly state which methods are thread safe and which are not to avoid any ambiguity.
See the final paragraph - .NET Class Library
All public static members (methods, properties, fields, and events) within the .NET Framework support concurrent access within a multithreaded environment. Therefore, any .NET Framework static member can be simultaneously invoked from two threads without encountering race conditions, deadlocks, or crashes.
For all classes and structures in the .NET Framework, check the Thread Safety section in the API reference documentation to determine whether it is thread safe. If you want to use a class that is not thread-safe in a multithreaded environment, you must wrap an instance of the class with code that supplies the necessary synchronization constructs.

Is it safe for multiple threads to read from a Lookup<TKey, TElement>?

Is it safe for multiple threads to read from a Lookup<TKey, TElement>?
Lookup<TKey, TElement> is immutable, however MSDN states:
Any public static (Shared in Visual Basic) members of this type are
thread safe. Any instance members are not guaranteed to be thread
safe.
Though I shudder to imagine it, I'm wondering if the machine that pumps out MSDN documentation
could be incorrect.
Because I don't like to risk that I may have to debug an obscure multithreading related bug 1 year from now, I'm going to assume it's not safe to use this class without manual synchronization.
As long as there is no writing, doing just reading is thread-safe. This is valid in any case.
Your question is in a sense orthogonal to the notion of thread-safety. A write in combination with a write or read is not thread-safe, but multiple reads without writing are thread-safe.
What MSDN says about instance members not being guaranteed to be thread-safe can only be valid in case of non-thread-safe scenarios, which by definition imply a write operation.
This is standard disclaimer for all most classes as you've probably noticed. Some methods may be thread safe, but "are not guaranteed".
Generally it is safe to read from collection using multiple threads if there are no writers to collection. If you need to update collection at the same time - use appropriate synchronization or built in thread safe collections like SynchronizedKeyedCollection.
Because the Lookup<TKey,TElement> is immutable, means that you will get the same values for all members. It does not mean that the items stored in it cannot be modified. So the collection is indeed not thread safe. A perfect example would be that most linq is lazy evaluated, and creating the enumerator could involve executing the lazy code. Trying to enumerate in two separate threads could cause the collection to be realized twice producing the wrong result.
Update:
Now that the source code is available on https://referencesource.microsoft.com it is confirmed that internal state is set during method calls without regard to multithreading meaning that you could have race conditions and the Lookup<TKey,TElement> class is in fact not thread safe.

MSpec: How to make static variables thread-safe?

I'm using MSpec for my latest project, and overall I'm really happy with it. However, I do have an issue with concurrency when my tests run in paralel and I'm wondering if anybody has run into this issue or, even better, has a solution?
MSpec heavily relies on static methods and variables to work.
Now it appears when I define static variables in my base classes, that are used by multiple test classes, and I run my tests in paralel, that they share the same static variables and thus interfere with eachother.
I'm using both NCrunch and Resharper as my testrunners and I'm experiencing the problem in both.
Anybody familiar with this problem?
Firstly, I would recommend reading the Thead Safety Guidelines on MSDN. This will give you a good overview of how and why to make methods thread safe in C#.
The following rules outline the design guidelines for implementing threading:
Avoid providing static methods that alter static state. In common server scenarios, static state is shared across requests, which means multiple threads can execute that code at the same time. This opens up the possibility for threading bugs. Consider using a design pattern that encapsulates data into instances that are not shared across requests.
... Adding locks to create thread-safe code decreases performance, increases lock contention, and creates the possibility for deadlock bugs to occur
Be aware of method calls in locked sections. Deadlocks can result when a static method in class A calls static methods in class B and vice versa. If A and B both synchronize their static methods, this will cause a deadlock. You might discover this deadlock only under heavy threading stress.
Be aware of issues with the lock statement (SyncLock in Visual Basic). It is tempting to use the lock statement to solve all threading problems. However, the System.Threading.Interlocked Class is superior for updates that must be atomic ...
As a general note a methodology which I prefer to use (where possible) is to make a method (static or otherwise) immutable. To do this, all variables should be local (created locally on the stack, or passed in as parameters to a method). By ensuring only local variables are used, or member variables are immutable each thread will operate in its own compartment and changes to variables will not affect another thread. This is a methodology I have used extensively in .NET simulation software to allow lock-less and therefore high performance multithreading in C#.
Alternatively, if variables must be member variables and mutable access to them may be protected by lock keywords. Be careful with the use of lock will cause context switching (slow down) and introduces the possibility of a deadlock situation. It also doesn't gaurantee thread safety as the use of lock must protect against the specific scenario you are trying to prevent.
For further reading I would suggest looking these related questions which describe thread safety and immutability in C#:
Designing a Thread Safe class
Achieving Thread Safety
Why are immutable objects thread safe
Best regards,
Static fields are not thread-safe by default. To make them thread-safe you can decorate them with the [ThreadStatic] attribute.
Have a look at ThreadStaticAttribute Class at MSDN for more info.

Which features make a class to be thread-safe?

In MSDN some .NET classes described like this:
"This type is thread safe."
or
"Public static (Shared in Visual Basic) members of this type are thread safe. Instance members are not guaranteed to be thread-safe.".
My question is which features make a class to be thread-safe?
Is there any standard, recommendation or guidelines for thread-safety programming?
When I use lock(C#) keyword, it means my class is thread-safe or not?
How to I evaluate thread-safety of a class? Is there any TESTS to be sure that a class is 100% thread safe?
Example:
public class MyClass
{
public void Method()
{
lock (this)
{
// Now, is my class 100% thread-safe like Microsoft classes?
}
}
type m_member1;
type m_member2;
}
thanks
Is there any standard, recommendation or guidelines for thread-safety
programming?
The most important standard is to ensure that all static members are thread-safe. You will see that all well written APIs including the .NET base class library makes this guarantee across the board. There is a really good reason for this. Since static members are shared across an AppDomain they could be used by many different threads without you even realizing it. It would be awkward at best to provide your own synchronization for every single static member access. Imagine what it would be like if Console.WriteLine were not thread-safe.
As far as recommendations and guidelines there are plenty of well established patterns for doing concurrent programming. The patterns that are out there cover a wide variety of programming problems and use many different synchronization mechanisms. The producer-consumer pattern is one of many well known patterns which happens to solve a large percentage of concurrent programming problems.
Read Threading in C# by Joseph Albahari. It is one of the best and most vetted resources available.
When I use lock(C#) keyword, it means my class is thread-safe or not?
Nope! There is no magic bullet that can make a class thread-safe. The lock keyword is but one of many different tools that can be used to make a class safe for simultaneous access by multiple threads. But, just using a lock will not guarantee anything. It is the correct use of synchronization mechanisms that makes code thread-safe. There are plenty ways to use these mechanisms incorrectly.
How to I evaluate thread-safety of a class? Is there any TESTS to be
sure that a class is 100% thread safe?
This is the million dollar question! It is incredibly difficult to test multithreaded code. The CHESS tool provided by Microsoft Research is one attempt at making life easier for concurrent programmers.
A class is generally considered thread-safe if its methods can be invoked by multiple threads concurrently without corrupting the state of the class or causing unexpected side-effects. There are many reasons why a class may not be thread safe, although some common reasons are that it contains some state that would be corrupted on concurrent access.
There are a number of ways to make a class thread-safe:
Make it immutable, if a class contains no state it is safe to use concurrently from multiple threads.
Employ locking to reduce concurrency. However, this is no guarantee of thread safety, it just ensures that a block of code will not be executed concurrently by multiple threads. If state is stored between method invocations this might still become inconsistent.
How you create a thread-safe class really depends on what you want to do with the class in question.
You also need to ask yourself, do I need to make my class threadsafe? a common model of most UI frameworks is that there is a single UI thread. For example in WinForms, WPF and Silverlight the majority of your code will be executed from the UI thread which means you do not have to build thread-safety into your classes.
First of all, don't use lock(this).
This can cause deadlocks. Because other code can lock that same object from outside the class' scope. You should create a local Object and use it as the class' lock.
Second, thread safety is a complicated issue. There's tons of material about this on the web.
As a rule of thumb, all public methods should be locked and thread safe for the class to be thread-safe.
A Class is considered thread safe if only one thread at a time can modify the state of the objects created from the class OR the class provide such functionality that multiple threads can call various methods of the class at same time.
When I use lock(C#) keyword, it means my class is thread-safe or not?
When you use lock it means that the portion of code inside the lock {} is thread safe. It doesn't guarantee that your class is thread safe. And as Yochai Timmer said it is not a good idea to lock(this)
How to I evaluate thread-safety of a class? Is there any TESTS to be sure that a class is 100% thread safe?
I am not sure there are any tests because it is always possible in multi-threading that you are by chance getting correct results. So in order to be sure you can go through the code of class to see how it is making sure it is thread safe
Very simple explanation:
Thread safe type means you don't need any additional synchronization mechanisms when using your type. Say you can create an instance pass a reference to another thread (or multiple threads) and use methods/properties from both threads without any additional overhead for thread safety.

List<T> concurrent removing and adding

I am not too sure, so i thought i'd ask. Would removing and adding items to a System.Collections.Generic.List<> object be non-thread safe?
My situation:
When a connection is received, it is added to the list, but also at the same time, there's a worker that's removing dead connections and such.
Is there a problem? Will a lock do?
I also want to know if i'm allowed to use a lock on the list object with it's Foreach<> method.
Yes, adding and removing items from a List<> is not thread safe, so you need to synchronise the access, for example using lock.
Mind that the lock keyword in no ways locks the object that you use as identifier, it only prevents two threads to enter the same code block at the same time. You will need locks around all code that accesses the list, using the same object as identifier.
At the time of the question there wasn't .NET Framework 4 yet, but the people who are faced the problem now should try to use collections from System.Collections.Concurrent namespace for dealing with thread-safe issues
List<T> is not thread-safe, so yes, you will need to control access to the list with a lock. If you have multiple threads accessing the List make sure you have them all respect the lock or you will have issues. The best way to do this would to be to subclass the List so that the locking happens automatically, else you will more than likely end up forgetting eventually.
Definitely using lock for particular code makes it thread safe, but I do not agree with it for current scenario.
You can implement method Synchronized to make collection thread safe. This link explains why and how to do that.
Another purely programmatic approach is mentioned in this link, though I never tested it firsthand but it should work.
btw, one of the bigger concern is that, are you trying to maintain something like connection pool on you own? if yes then why?
I take my answer back. Using locks in better answer that using this method.
Actually, sometimes List<> is thread-safe, and sometimes not, according to Microsoft:
Public static members of this type are
thread safe. Any instance members are
not guaranteed to be thread safe.
but that page goes on to say:
Enumerating through a collection is
intrinsically not a thread-safe
procedure. In the rare case where an
enumeration contends with one or more
write accesses, the only way to ensure
thread safety is to lock the
collection during the entire
enumeration. To allow the collection
to be accessed by multiple threads for
reading and writing, you must
implement your own synchronization.

Categories

Resources