Does the C# Yield free a lock?

拟墨画扇 提交于 2019-12-20 09:29:37

问题


I have the following method:

public static IEnumerable<Dictionary<string, object>> GetRowsIter
   (this SqlCeResultSet resultSet)
{
    // Make sure we don't multi thread the database.
    lock (Database)
    {
        if (resultSet.HasRows)
        {
            resultSet.Read();

            do
            {
                var resultList = new Dictionary<string, object>();
                for (int i = 0; i < resultSet.FieldCount; i++)
                {
                    var value = resultSet.GetValue(i);
                    resultList.Add(resultSet.GetName(i), value == DBNull.Value 
                                                                  ? null : value);
                }
                yield return resultList;
            } while (resultSet.Read());
        }
        yield break;
    }

I just added the lock(Database) to try and get rid of some concurancy issues. I am curious though, will the yield return free the lock on Database and then re-lock when it goes for the next iteration? Or will Database remain locked for the entire duration of the iteration?


回答1:


No the yield return will not cause any locks to be freed / unlocked. The lock statement will expand out to a try / finally block and the iterator will not treat this any differently than an explicit try / finally in the iterator method.

The details are a bit more complicated but the basic rules for when a finally block will run inside an iterator method is

  1. When the iterator is suspended and Dispose is called the finally blocks in scope at the point of the suspend will run
  2. When the iterator is running and the code would otherwise trigger a finally the finally block runs.
  3. When the iterator encounters a yield break statement the finally blocks in scope at the point of the yield break will run



回答2:


The lock translates to try/finally (normal c#)

In Iterator blocks (aka yield), "finally" becomes part of the IDisposable.Dispose() implementation of the enumerator. This code is also invoked internally when you consume the last of the data.

"foreach" automatically calls Dispose(), so if you consume with "foreach" (or regualar LINQ etc) it will get unlocked.

However, if the caller uses GetEnumerator() directly (very rare) and doesnt read all the data and doesn't call Dispose() then the lock will not be released.

I would have to check to see if or gets a finaliser; it might get released by GC, but I wouldn't bet money on it.




回答3:


The Database object will be locked until iteration finishes (or the iterator is disposed).

This might lead to excessive periods of locking, and I would recommend against doing it like this.




回答4:


The lock remains in effect until you get outside of the scope of lock(). Yielding does not do that.



来源:https://stackoverflow.com/questions/4608215/does-the-c-sharp-yield-free-a-lock

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!