This is not an unreasonable thing. There are times when methods that can cause thread safety problems in combination with other methods are safe if they are the only method.
However, this clearly does not apply to this case when you consider the code shown in the reflector:
public void Add(T item) { if (this._size == this._items.Length) { this.EnsureCapacity(this._size + 1); } this._items[this._size++] = item; this._version++; }
Even if EnsureCapacity itself was thread safe (and this certainly is not), the above code will clearly not be thread safe, given the possibility of simultaneous calls to the increment operator that causes an incorrect write.
Either block, or use a ConcurrentList, or perhaps use an unblocked queue, as a place where many threads are written to, and read from it - either directly or by filling out a list with it - after they have done their job (I ' m, assuming that multiple simultaneous writes, followed by single-threaded reading, is your template here, judging by your question, because otherwise I donโt see how the condition can be used when Add is the only method that can be used van).
Jon Hanna Apr 08 '11 at 1:12
source share