3

我正在尝试测试 C#parallel方法,这是我的测试程序:

class Program
{
    static int counter;
    static void Main(string[] args)
    {
        counter = 0;
        Parallel.Invoke(
            () => func(1),
            () => func(2),
            () => func(3)
            );
        Console.Read();
    }


    static void func(int num)
    {
        for (int i = 0; i < 5;i++ )
        {
            Console.WriteLine(string.Format("This is function #{0} loop. counter - {1}", num, counter));
            counter++;
        }
    }
}

我试图做的是拥有 1 个静态共享变量,每个函数实例都会将其增加 1。

我预计counter会按顺序打印(1,2,3,...) 但输出令人惊讶:

This is function #1 loop. counter - 0
This is function #1 loop. counter - 1
This is function #1 loop. counter - 2
This is function #1 loop. counter - 3
This is function #1 loop. counter - 4
This is function #3 loop. counter - 5
This is function #2 loop. counter - 1
This is function #3 loop. counter - 6
This is function #3 loop. counter - 8
This is function #3 loop. counter - 9
This is function #3 loop. counter - 10
This is function #2 loop. counter - 7
This is function #2 loop. counter - 12
This is function #2 loop. counter - 13
This is function #2 loop. counter - 14

谁能向我解释为什么会这样?

4

3 回答 3

2

问题是您的代码不是线程安全的。例如,可能发生的情况是:

  • 函数 #2 获取counter使用它的值Console.WriteLine()
  • 函数 #1 获取counter, 调用Console.WriteLine(), 递增的值counter
  • 函数 #1 获取counter, 调用Console.WriteLine(), 递增的值counter
  • Console.WriteLine()函数 #2 最终使用旧值调用

此外,++它本身不是线程安全的,因此最终值可能不是 15。

要解决这两个问题,您可以使用Interlocked.Increment()

for (int i = 0; i < 5; i++)
{
    int incrementedCounter = Interlocked.Increment(ref counter);
    Console.WriteLine("This is function #{0} loop. counter - {1}", num, incrementedCounter);
}

这样,您将在增量之后获得数字,而不是在原始代码中之前。此外,此代码仍不会以正确的顺序打印数字,但您可以确定每个数字将仅打印一次。

如果您确实希望以正确的顺序排列数字,则需要使用lock

private static readonly object lockObject = new object();

…

for (int i = 0; i < 5; i++)
{
    lock (lockObject)
    {
        Console.WriteLine("This is function #{0} loop. counter - {1}", num, counter);
        counter++;
    }
}

当然,如果你这样做,你实际上不会得到任何并行性,但我认为这不是你真正的代码。

于 2013-05-01T13:50:47.907 回答
1

实际上会发生什么 - Invoke 只是将这些任务排队,运行时为这些任务分配线程,这给它提供了很多随机元素(哪个将首先被拾取等)。

甚至 msdn 文章都这样说:

此方法可用于执行一组操作,可能是并行的。不保证操作执行的顺序或它们是否并行执行。在每个提供的操作完成之前,此方法不会返回,无论完成是由于正常终止还是异常终止。

于 2013-05-01T11:46:25.653 回答
0

这个问题看起来像许多线程访问同一个变量。这是并发问题。你可以试试:

    static object syncObj = new object();
    static void func(int num)
    {
        for (int i = 0; i < 5; i++)
        {
            lock (syncObj)
            {
                Console.WriteLine(string.Format("This is function #{0} loop. counter - {1}", num, counter));
                counter++;
            }
        }
    }
于 2013-05-01T13:45:26.743 回答