C#: How can I make an IEnumerable thread safe?

There’s an inherent problem in doing so, because IEnumerator<T> has both MoveNext() and Current. You really want a single call such as:

bool TryMoveNext(out T value)

at that point you can atomically move to the next element and get a value. Implementing that and still being able to use yield could be tricky… I’ll have a think about it though. I think you’d need to wrap the “non-threadsafe” iterator in a thread-safe one which atomically performed MoveNext() and Current to implement the interface shown above. I don’t know how you’d then wrap this interface back into IEnumerator<T> so that you could use it in foreach though…

If you’re using .NET 4.0, Parallel Extensions may be able to help you – you’d need to explain more about what you’re trying to do though.

This is an interesting topic – I may have to blog about it…

EDIT: I’ve now blogged about it with two approaches.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)