I have an enumerator written in C#, which looks something like this:
try
{
ReadWriteLock.EnterReadLock();
yield return foo;
yield return bar;
yield return bash;
}
finally
{
if (ReadWriteLock.IsReadLockHeld)
ReadWriteLock.ExitReadLock();
}
I believe this may be a dangerous locking pattern, as the ReadWriteLock will only be released if the enumeration is complete, otherwise the lock is left hanging and is never released, am I correct? If so, what’s the best way to combat this?
No, the
finallyblock will always be executed, pretty much unless somebody pulls the plug from the computer (well and a few other exceptions).…
The output of the above will be
Note that in C# you write
yield return, not justyield. But I guess that was just a typo.