I need a thread-safe version of a SortedList<T,U>, but unlike its generic form the non-generic form doesn’t appear to have Synchronized functionality. Is there a different trick I can use to get a thread-safe version? Or will I have to resort to using lock (SyncRoot) manually?
I need a thread-safe version of a SortedList<T,U> , but unlike its generic form
Share
Short answer: no you have to resort
lock.Even if in the new namespace
System.Collections.Concurrentyou have thread safe collections the thread safe version ofSortedList<T, U>isn’t there.If you need a to protect each call to
SortedList<T, U>with alockor aReadWriterLockSlim(when applicable) or, in alternative, to create a thread safe wrapper forSortedList<T, U>(in the same waySynchronized()does).Longer answer: no you do not have anything ready but you can achieve the same result using together
OrderablePartitioner<TSource>(to extract one item per time) andConcurrentBag<T>. Frankly speaking I don’t know if there is any ROI with this strategy because if you do not need a partitioner then you simply add complexity to something shouldn’t be.