If I have something like this (pseudocode):
class A
{
List<SomeClass> list;
private void clearList()
{
list = new List<SomeClass>();
}
private void addElement()
{
list.Add(new SomeClass(...));
}
}
is it possible that I run into multithreading problems (or any kind of unexpected behavior) when both functions are executed in parallel?
The use case is a list of errors, which could be cleared at any time (by simply assigning a new, empty list).
EDIT: My assumptions are
There are two possibilities for problems here:
AddElement
and ClearList
are called at the same time, you have a race condition: either the element will end up in the new list, or in the old (forgotten) one.List<T>
isn't safe for multi-threaded mutation, so if two different threads call AddElement
at the same time the results aren't guaranteedGiven that you're accessing a shared resource, I would personally hold a lock while accessing it. You'll still need to consider the possibility of clearing the list immediately before/after adding an item though.
EDIT: My comment about it being okay if you're only adding from one thread was already somewhat dubious, for two reasons:
List<T>
which hadn't been fully constructed yet. I'm not sure, and the .NET 2.0 memory model (as opposed to the one in the ECMA specification) may be strong enough to avoid that, but it's tricky to say.list
variable immediately, and still add to the old list. Indeed, without any synchronization, it could see the old value foreverWhen you add "iterating in the GUI" into the mix it gets really tricky - because you can't change the list while you're iterating. The simplest solution to this is probably to provide a method which returns a copy of the list, and the UI can safely iterate over that:
class A
{
private List<SomeClass> list;
private readonly object listLock = new object();
private void ClearList()
{
lock (listLock)
{
list = new List<SomeClass>();
}
}
private void AddElement()
{
lock (listLock)
{
list.Add(new SomeClass(...));
}
}
private List<SomeClass> CopyList()
{
lock (listLock)
{
return new List<SomeClass>(list);
}
}
}
Yes - it is possible,. In fact, if these are genuinely being called at the same time, it is highly likely.
In addition, it is also likely to cause problems if two seperate calls to addElement occur at the same time.
For this sort of multithreading, you really need some sort of mutually exclusive lock around the list itself, so only one operation on the underlying list can be called at a time.
A crude locking strategy around this would help. Something like:
class A
{
static object myLock = new object()
List<SomeClass> list;
private void clearList()
{
lock(myLock)
{
list = new List<SomeClass>();
}
}
private void addElement()
{
lock(myLock)
{
list.Add(new SomeClass(...));
}
}
}
Collections in .NET (up to 3.5) are not thread-safe or non-blocking (parallel execution). You should implement yours by deriving from IList and use a ReaderWriterLockSlim for performing every action. For example, your Add method should look like this:
public void Add(T item)
{
_readerWriterLockSlim.EnterWriteLock();
try { _actualList.Add(item); }
finally { _readerWriterLockSlim.ExitWriteLock(); }
}
You must be aware of some concurrency tricks here. For example you must have a GetEnumerator which returns a new instance as an IList; not the actual list. Otherwise you will run into problems; which should look like:
public IEnumerator<T> GetEnumerator()
{
List<T> localList;
_lock.EnterReadLock();
try { localList= new List<T>(_actualList); }
finally { _lock.ExitReadLock(); }
foreach (T item in localList) yield return item;
}
and:
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return ((IEnumerable<T>)this).GetEnumerator();
}
Note: When implementing thread-safe or parallel collections (and in fact every other class) DO NOT DERIVE FROM THE CLASS, BUT INTERFACE! Because there will be always problems related to internal structure of that class or some methods that are not virtual and you have to hide them and so on. If you have to do this, do it very carefully!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With