Skip to content

Theraot.Collections.Progressor

Alfonso J. Ramos edited this page Nov 10, 2015 · 2 revisions

Progressor<T>

This class allows to consume the contents of a collection by multiple threads in such way that no item is taken by multiple threads, but without the need of additional coordination between the threads.

The collection to be consumed can be another Progressor<T>, and IEnumerable<T> and array T[] and IObservable<T> or given by a bool TryTake<T> (out T item) delegate.

To use an object of this class, each consumer must call the TryTake method which will return true if an item was retrieved. It must be noted that even if TryTake returns false, it doesn't mean that there are will not be more items... it may be the case that the underlaying collection is an IObservable<T> that hasn't send any more items. To make sure that there will not be more items, the consumer must check the IsClosed property.

In addition to that, Progressor<T> itself implements IObservable<T> that will give a notificiation each time an item is consumed.

Progressor<T> can be casted to IEnumerable<T> via the AsEnumerable method. When doing so, it must be kept in mind that any particular item will never be enumerated twice.

Also, when using the While methods it must be noted that the first item to not match the condition will have been consumed, so it is appropiate to procces it in the callback.


Progressor<T> is used to implement the "Progresive" types, which are:

  • ProgressiveCollection<T>
  • ProgressiveDictionary<T> (FAT)
  • ProgressiveList<T>
  • ProgressiveLookup<T> (FAT)
  • ProgressiveSet<T> (FAT)

These work by taking a collection in the form of an IEnumeralbe<T> and providing an extended set of methods to work with it. Also, they are and they assume that the given collection is readonly. Unders this assumption it makes sense to cache the IEnumerable<T> but since it maybe very large or potentially infinite... you only want to cache it progressively (hence the name) Progressor<T> allows to do this, even when multiple threads are using the same object.

There is no guarantee for the progressive types to be thread safe if the underlaying collection is not thread safe.