gpt4 book ai didi

c# - 使用并行时程序卡住

转载 作者:太空宇宙 更新时间:2023-11-03 13:09:21 25 4
gpt4 key购买 nike

我是 C# 的新手,我编写了一个模拟乐透抽奖的简单程序。它采用第一个(随机)数字并计算获胜所需的抽奖次数。这是波兰乐透,所以有 6 个号码可以匹配。

当程序在简单的 for 循环中运行时,一切正常。但是,当我使用 Parallel For 或任何其他多任务处理或多线程选项时,就会出现问题。

首先是代码:

class Program
{
public static int howMany = 100;

static void Main(string[] args)
{

Six my;
Six computers;
long sum = 0;
double avg = 0;
int min = 1000000000;
int max = 0;



for (int i = 0; i < howMany; i++)
{
my = new Six();
Console.WriteLine((i + 1).ToString() + " My: " + my.ToString());

int counter = 0;
do
{
computers = new Six();
counter++;
} while (!my.Equals(computers));
Console.WriteLine((i + 1).ToString() + " Computers: " + computers.ToString());
Console.WriteLine(counter.ToString("After: ### ### ###") + "\n");
if (counter < min)
min = counter;
if (counter > max)
max = counter;
sum += counter;
}

avg = sum / howMany;
Console.WriteLine("Average: " + avg);
Console.WriteLine("Sum: " + sum);
Console.WriteLine("Min: " + min);
Console.WriteLine("Max: " + max);

Console.Read();
}

}







class Six : IEquatable<Six>
{
internal byte first;
internal byte second;
internal byte third;
internal byte fourth;
internal byte fifth;
internal byte sixth;
private static Random r = new Random();

public Six()
{

GenerateRandomNumbers();
}

public bool Equals(Six other)
{
if (this.first == other.first
&& this.second == other.second
&& this.third == other.third
&& this.fourth == other.fourth
&& this.fifth == other.fifth
&& this.sixth == other.sixth)
return true;
else
return false;
}

private void GenerateRandomNumbers()
{
byte[] numbers = new byte[6];
byte k = 0;
for (int i = 0; i < 6; i++)
{
do
{
k = (byte)(r.Next(49) + 1);
}while (numbers.Contains(k));

numbers[i] = k;
k = 0;
}

Array.Sort(numbers);

this.first = numbers[0];
this.second = numbers[1];
this.third = numbers[2];
this.fourth = numbers[3];
this.fifth = numbers[4];
this.sixth = numbers[5];
}

public override string ToString()
{
return this.first + ", " + this.second + ", " + this.third + ", " + this.fourth + ", " + this.fifth + ", " + this.sixth;
}

}

当我尝试让它成为 Parallel.For 时:

        long sum = 0;
double avg = 0;
int min = 1000000000;
int max = 0;


Parallel.For(0, howMany, (i) =>
{
Six my = new Six();
Six computers;
Console.WriteLine((i + 1).ToString() + " My: " + my.ToString());

int counter = 0;
do
{
computers = new Six();

// Checking when it's getting stuck
if (counter % 100 == 0)
Console.WriteLine(counter);

counter++;
} while (!my.Equals(computers));
Console.WriteLine((i + 1).ToString() + " Computers: " + computers.ToString());
Console.WriteLine(counter.ToString("After: ### ### ###") + "\n");

// It never get to this point, so there is no problem with "global" veriables
if (counter < min)
min = counter;
if (counter > max)
max = counter;
sum += counter;
});

程序在某个时候卡住了。计数器达到 ~3,000-40,000 并且拒绝进一步增加。

我尝试过的:

  1. 让类成为一个结构
  2. 每~1000 次迭代收集一次垃圾
  3. 使用线程池
  4. 使用 Task.Run
  5. 只让随机类(class)计划成员(厌倦了让六类(class)“更轻”)

但我一无所获。

我知道这对你们中的一些人来说可能是一件非常简单的事情,但是人们必须以某种方式学习 ;) 我什至买了一本关于异步编程的书来找出它为什么不起作用,但无法理解出。

最佳答案

Random 不是线程安全的...

等待您的代码在并行版本中停止写入新行并暂停。这将停止所有线程。您会注意到所有并行线程都在 while 循环中。

数字数组都是 1,0,0,0,0,0 并且 r.Next 只返回 1。字节数组总是包含它。所以,你破坏了 Random

要解决此问题,您需要使 r 线程安全,方法是在每次访问 r.Next 时锁定 r 或将静态声明更改为

private static readonly ThreadLocal<Random> r
= new ThreadLocal<Random>(() => new Random());

Next 调用变为

k = (byte)(r.Value.Next(49) + 1);

这将为每个线程创建一个新的静态随机实例。

如您所述,同时创建大量随机数会产生相同的数字序列,为解决此问题,请添加一个种子类

static class RGen
{
private static Random seedGen = new Random();

public static Random GetRGenerator()
{
lock (seedGen)
{
return new Random(seedGen.Next());
}
}
}

并将声明更改为

private static readonly ThreadLocal<Random> r
= new ThreadLocal<Random>(() => RGen.GetRGenerator());

这将确保每个新的随机实例都有不同的种子值。

关于c# - 使用并行时程序卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29671014/

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