Is yield return in C# thread-safe?

What exactly do you mean by thread-safe?

You certainly shouldn’t change the dictionary while you’re iterating over it, whether in the same thread or not.

If the dictionary is being accessed in multiple threads in general, the caller should take out a lock (the same one covering all accesses) so that they can lock for the duration of iterating over the result.

EDIT: To respond to your edit, no it in no way corresponds to the lock code. There is no lock automatically taken out by an iterator block – and how would it know about syncRoot anyway?

Moreover, just locking the return of the IEnumerable<TKey> doesn’t make it thread-safe either – because the lock only affects the period of time when it’s returning the sequence, not the period during which it’s being iterated over.

Leave a Comment

techhipbettruvabetnorabahisbahis forumueduedueduedueduseduseduedusedusedu