c# - 为什么递减一个变量会修改 C# Parallel.ForEach 循环中另一个变量的递增?

标签 c# linq concurrency lambda task-parallel-library

运行来自 Parallel.ForEach keeps spawning new threads 的代码经过我的一些修改

带注释行的输出:

//threadsRemaining = Interlocked.Decrement(ref concurrentThreads);

是“显而易见的”,即预期的:

[00:00] Job 0 complete. 2 threads remaining. unsafeCount=2
[00:00] Job 1 complete. 1 threads remaining. unsafeCount=1
[00:00] Job 2 complete. 3 threads remaining. unsafeCount=3
[00:00] Job 3 complete. 4 threads remaining. unsafeCount=4
[00:00] Job 4 complete. 5 threads remaining. unsafeCount=5
[00:00] Job 5 complete. 6 threads remaining. unsafeCount=6
[00:01] Job 6 complete. 7 threads remaining. unsafeCount=7
[00:01] Job 8 complete. 8 threads remaining. unsafeCount=8
[00:01] Job 7 complete. 9 threads remaining. unsafeCount=9
[00:01] Job 9 complete. 10 threads remaining. unsafeCount=10

虽然取消注释以上行时相同代码的输出是:

[00:00] Job 0 complete. 1 threads remaining. unsafeCount=1
[00:00] Job 1 complete. 0 threads remaining. unsafeCount=0
[00:00] Job 3 complete. 0 threads remaining. unsafeCount=0
[00:00] Job 2 complete. 1 threads remaining. unsafeCount=1
[00:00] Job 4 complete. 1 threads remaining. unsafeCount=1
[00:00] Job 5 complete. 1 threads remaining. unsafeCount=1
[00:01] Job 6 complete. 1 threads remaining. unsafeCount=1
[00:01] Job 8 complete. 1 threads remaining. unsafeCount=1
[00:01] Job 9 complete. 1 threads remaining. unsafeCount=1
[00:01] Job 7 complete. 0 threads remaining. unsafeCount=0

你能解释一下为什么递减一个变量 threadsRemainin 会停止(或阻止)递增另一个变量 unsafeCount 吗?

控制台应用代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace seParallelForEachKeepsSpawningNewThreads
{
  public class Node
  {
    public Node Previous { get; private set; }
    public Node(Node previous)
    {
      Previous = previous;
    }
  }
  public class Program
  {
    public static void Main(string[] args)
    {
      DateTime startMoment = DateTime.Now;
      int concurrentThreads = 0;
      int unsafeCount = 0;

      var jobs = Enumerable.Range(0, 10);
      ParallelOptions po = new ParallelOptions
      {
        MaxDegreeOfParallelism = Environment.ProcessorCount
      };
      Parallel.ForEach(jobs, po, delegate(int jobNr)
      {
        int threadsRemaining = Interlocked.Increment(ref concurrentThreads);
        unsafeCount++;

        int heavyness = jobNr % 9;

        //Give the processor and the garbage collector something to do...
        List<Node> nodes = new List<Node>();
        Node current = null;
        //for (int y = 0; y < 1024 * 1024 * heavyness; y++)
        for (int y = 0; y < 1024 * 4 * heavyness; y++)
        {
          current = new Node(current);
          nodes.Add(current);
        }

        TimeSpan elapsed = DateTime.Now - startMoment;
//*****************
         //threadsRemaining = Interlocked.Decrement(ref concurrentThreads);
        Console.WriteLine("[{0:mm\\:ss}] Job {1} complete. {2} threads remaining. unsafeCount={2}", 
          elapsed, jobNr, threadsRemaining, unsafeCount);
      });
      Console.WriteLine("FINISHED");
      Console.ReadLine();
    }
  }
}

最佳答案

问题是:

Console.WriteLine(
    "[{0:mm\\:ss}] Job {1} complete. {2} threads remaining. unsafeCount={2}", 
    elapsed, jobNr, threadsRemaining, unsafeCount);

最后一部分应该是 {3},而不是 {2}。您现在只是打印出 threadsRemaining 两次...

关于c# - 为什么递减一个变量会修改 C# Parallel.ForEach 循环中另一个变量的递增?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15380059/

相关文章:

c# - 如何限制c#中的最大并行任务数

c# - 如何在数据库表中查找连接的组合框中所选项目的索引

c# - 两台不同的 Azure 服务器上的相同代码,但其中一台存在路由问题?

c# - LINQ 的问题 - 有必要添加对不需要的库的引用

c# - 有条件的

loops - 去审核-循环变量我被func文字捕获

python - 使用 asyncio 发出 100 个请求后,并行请求会无限阻塞

java - 与 Java 或其他非函数式语言相比,Scala 究竟如何利用更多的核心?

c# - 嵌套使用是个好主意

c# - 使用基于属性的 LINQ 从列表中选择对象