Is this a dangerous locking pattern?

Posted by Martin on Stack Overflow See other posts from Stack Overflow or by Martin
Published on 2010-06-02T10:20:53Z Indexed on 2010/06/02 10:23 UTC
Read the original article Hit count: 337

Filed under:
|
|

I have an enumerator written in C#, which looks something like this:

try
{
    ReadWriteLock.EnterReadLock();
    yield foo;
    yield bar;
    yield bash;
}
finally
{
    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?

© Stack Overflow or respective owner

Related posts about c#

Related posts about locking