5

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

  • only one thread adds elements
  • forgotten elements are okay (i.e. race condition between clearing and adding a new element), as long as the clear operation succeeds without problems
  • .NET 2.0
4

6 回答 6

10

There are two possibilities for problems here:

  • Newly added items could end up being forgotten immediately, because you clear out and create a new list. Is that an issue? Basically, if 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 guaranteed

Given 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:

  • It's possible (I think!) that you could end up trying to add to a 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.
  • It's possible that the adding thread wouldn't "see" the change to the list variable immediately, and still add to the old list. Indeed, without any synchronization, it could see the old value forever

When 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);
        }
    }

}
于 2010-02-13T13:03:46.460 回答
2

.NET(最高 3.5)中的集合不是线程安全的或非阻塞的(并行执行)。您应该通过派生 IList 来实现您的,并使用 ReaderWriterLockSlim 来执行每个操作。例如,您的 Add 方法应如下所示:

    public void Add(T item)
    {
        _readerWriterLockSlim.EnterWriteLock();
        try { _actualList.Add(item); }
        finally { _readerWriterLockSlim.ExitWriteLock(); }
    }

您必须了解这里的一些并发技巧。例如,您必须有一个 GetEnumerator,它以 IList 形式返回一个新实例;不是实际的清单。否则你会遇到问题;这应该看起来像:

    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;
    }

和:

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return ((IEnumerable<T>)this).GetEnumerator();
    }

注意:在实现线程安全或并行集合(实际上是其他所有类)时,不要从类派生,而是从接口派生!因为总会存在与该类的内部结构或某些非虚拟方法相关的问题,您必须隐藏它们等等。如果您必须这样做,请非常小心!

于 2010-02-13T15:38:36.767 回答
2

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(...));
        }
    }
}
于 2010-02-13T13:04:19.910 回答
1

It is properly not a good thing to just make a new List when you want to clear it.

I assume you also assigned list in the constructor so you don't run into a null-pointer exception.

If you clear and elements is added, they can be added to the old list which I assume is fine? BUT if two elements is added at the same time, you can run into problems.

Look into .Net 4 new collections to handle multithreading tasks :)

ADDITION: Look into the namespace System.Collections.Concurrent if you use .Net 4. There you will find: System.Collections.Concurrent.ConcurrentBag<T> and many other nice collections :)

You should also note that lock can significantly pull down performance if you dont watch out.

于 2010-02-13T13:05:31.483 回答
1

If you use one instance of this class in multiple threads, yes. you will run into problems. All collections in the .Net framework (version 3.5 and lower) are NOT thread-safe. Specially when you start changing the collection while another thread is itterating over it.

Use locking and give out ´copies of´ collections in multithreaded environments, or if you can use .Net 4.0, use the new concurrent collections.

于 2010-02-13T13:06:30.720 回答
0

从对您问题的编辑中可以清楚地看出,您并不真正关心这里通常的罪魁祸首 - 实际上没有同时调用同一对象的方法。

本质上,您是在询问是否可以在从并行线程访问列表时将引用分配给您的列表。

据我了解,它仍然会造成麻烦。这一切都取决于如何在硬件级别上实现参考分配。更准确地说,这个操作是否是原子的。

我认为尽管它很渺茫,但仍然有机会,特别是在多处理器环境中,该进程将被损坏的引用,因为它在访问它时只是部分更新。

于 2010-02-13T13:50:13.417 回答