简体   繁体   English

访问修改后的关闭问题

[英]Access to modified closure question

Have had a bit of a search, but couldn't find the same situation I'm facing here. 进行了一些搜索,但找不到我在这里遇到的相同情况。

Here's the snippet I'm worried about: 这是我担心的片段:

    var threadsafeVacantStats = new Dictionary<Thread, StatsNode>();
    var threadSafeVacantLocker = new Object();
    var threadsafeZoneStats = new Dictionary<Thread, StatsNode>();
    var threadSafeZoneLocker = new Object();
    Parallel.ForEach(m_TeamAreasByZone[zone.ContainerID], team =>
    {
        var tempVacantStatNode = CreateASyncStatsNodes(threadSafeVacantLocker, threadsafeVacantStats);
        var tempZoneStatNode = CreateASyncStatsNodes(threadSafeZoneLocker, threadsafeZoneStats);
        //...other stuff
    }

Here's the function it's calling: 这是它正在调用的函数:

private static StatsNode CreateASyncStatsNodes(object threadSafeLocker, Dictionary<Thread, StatsNode> threadsafeTeamStats)
{
    StatsNode tempStatsNode;
    var currentThread = Thread.CurrentThread;
    lock (threadSafeLocker)
    {
        if (!threadsafeTeamStats.ContainsKey(currentThread))
            threadsafeTeamStats[currentThread] = new StatsNode(0, 0);

        tempStatsNode = threadsafeTeamStats[currentThread];
    }
    return tempStatsNode;
}

To me, this looks fine, however resharper is giving a warning for the second call to CreateASyncStatsNodes (the first call is fine). 对我来说,这看起来不错,但是resharper会在第二次调用CreateASyncStatsNodes时发出警告(第一次调用很好)。 Following its advice, it turns the block into: 按照其建议,它将块变成:

var threadsafeVacantStats = new Dictionary<Thread, StatsNode>();
var threadSafeVacantLocker = new Object();
var threadsafeZoneStats = new Dictionary<Thread, StatsNode>();
var threadSafeZoneLocker = new Object();
object locker = threadSafeZoneLocker;
Dictionary<Thread, StatsNode> stats = threadsafeZoneStats;
Parallel.ForEach(m_TeamAreasByZone[zone.ContainerID], team =>
    {
        var tempVacantStatNode = CreateASyncStatsNodes(threadSafeVacantLocker, threadsafeVacantStats);
        var tempZoneStatNode = CreateASyncStatsNodes(locker, stats);
        //...
    }

This doesn't really make sense to me, and doesn't seem to actually fix anything (if it was even broken in the first place). 这对我来说真的没有任何意义,并且似乎并没有真正解决任何问题 (如果它一开始就被破坏了)。 Is resharper mistakenly marking giving a warning, or am I missing something? 是reshaper错误地标记了警告,还是我错过了什么?

Resharper doesn't know that Parallel.ForEach execute passed lambda immediately. Resharper不知道Parallel.ForEach执行立即传递了lambda。 It suppose that this lambda could be executed later, when closure will be modified and this could produce some problems. 它假定可以在稍后修改闭包时执行此lambda,这可能会产生一些问题。 You can ignore this warning. 您可以忽略此警告。

Exactly what warning is ReSharper giving you? ReSharper到底给您什么警告? Keep in mind static analysis isn't perfect, I would say it's very likely ReSharper is making a mistake. 请记住,静态分析并不完美,我会说ReSharper很可能会犯错。 The original code looks fine to me. 原始代码对我来说看起来不错。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM