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 finally
block will always be executed, pretty much unless somebody pulls the plug from the computer (well and a few other exceptions).
public static IEnumerable<int> GetNumbers() {
try
{
Console.WriteLine("Start");
yield return 1;
yield return 2;
yield return 3;
}
finally
{
Console.WriteLine("Finish");
}
}
...
foreach(int i in GetNumbers()) {
Console.WriteLine(i);
if(i == 2) break;
}
The output of the above will be
Start
1
2
Finish
Note that in C# you write yield return
, not just yield
. But I guess that was just a typo.
I think David's answered the question you intended to ask (about the enumeration aspect), but two additional points to consider:
ReadWriteLock.EnterReadLock
threw an exception?ReadWriteLock.ExitReadLock
threw an exception?In #1, you'll call ReadWriteLock.ExitReadLock
inappropriately. In #2, you may hide an existing exception that's been thrown (since finally
clauses happen either because the mainline processing reached the end of the try
block or because an exception was thrown; in the latter case, you probably don't want to obscure the exception). Perhaps both of those things are unlikely in this specific case, but you asked about the pattern, and as a pattern it has those issues.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With