gpt4 book ai didi

c# - Parallel.For 没有正确处理锁

转载 作者:行者123 更新时间:2023-11-30 14:01:00 26 4
gpt4 key购买 nike

我做了以下测试:

private static object threadLocker = new object();

private static long threadStaticVar;
public static long ThreadStaticVar
{
get
{
lock (threadLocker)
{
return threadStaticVar;
}
}
set
{
lock (threadLocker)
{
threadStaticVar = value;
}
}
}

Parallel.For(0, 20000, (x) =>
{
//lock (threadLocker) // works with this lock
//{
ThreadStaticVar++;
//}
});

这个 Parallel.For 调用方法传递从 0 到 19999 的值。所以它会执行 20k 次。

如果我不使用 lock 包装 ThreadStaticVar++;,即使它在其 getset 上有锁,结果不会是20000。如果我删除注释栏并将其锁定在 .For 中,它会获得正确的值。

我的问题是:它是如何工作的?为什么 getset 上的锁不起作用?为什么它只在我的 For 中有效?

最佳答案

++ 运算符不是原子增量。将调用 get,然后调用 set,这些调用可以在不同线程之间交错进行,因为锁定仅针对每个单独的操作。可以这样想:

lock {tmp = var}
lock {var = tmp+1}

那些锁现在看起来不那么有效了,是吗?

关于c# - Parallel.For 没有正确处理锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9338358/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com