C++中多线程的混乱

Confusion in Multithreading in C++

本文关键字:混乱 多线程 C++      更新时间:2023-10-16

我正在尝试模拟一个概率问题,其中有n个客户端和n个服务器。每个客户端随机向任何服务器发送一个请求,因此每个服务器都可以接收任意数量的请求,我必须计算任何服务器可以接收的预期最大请求数。

我试图通过运行 10,000 次迭代来模拟这一点,在每次迭代中,每个客户端选择一个随机服务器并向其发送请求,服务器表示为大小为 N 的整数数组。

客户端选择一个随机数,然后服务器数组中该索引处的值递增。 因为,为了获得更好的结果,问题说N应该大约是 106

所以为了让它更快一点,我使用了多线程,其中每个线程运行 100 次迭代,总共有 10 个线程。

但是多线程代码产生的结果与普通代码的结果非常不同。 以下是带有输出的代码片段

普通版

#include <iostream>
#include <random>
#include <chrono>
#define N 1000000
#define iterations 1000
int servers[N];
// This array's i'th index will contain count of in how many
// iterations was i the maximum number of requests received by any  server
int distr[N+1]={0};
int main(int argc, char const *argv[])
{   
// Initialising
auto start = std::chrono::high_resolution_clock::now();
std::srand(time(NULL));
// Performing iterations
for(int itr=1; itr<=iterations; itr++)
{
for(int i=0;i<N;i++)
{
servers[i]=0;
}
for(int i=1;i<=N;i++)
{
int index = std::rand()%N;
servers[index]++;
}
int maxRes = -1;
for(int i=0;i<N;i++)
{
maxRes = std::max(maxRes, servers[i]);
}
distr[maxRes]+=1;
}
for(int i=0;i<=15;i++)
{
std::cout<<(double)distr[i]<<std::endl;
}
auto stop = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start);
std::cout<<duration.count()<<" milliseconds"<<std::endl;
return 0;
}

输出

0
0
0
0
0
0
0
359
552
79
10
0
0
0
0
0
1730 milliseconds

多线程版本

#include <iostream>
#include <random>
#include <chrono>
#include <thread>
#include <fstream>
#define N 100000
#define iterations 1000
#define threads 10
// This array's i'th index will contain count of in how many
// iterations was i the maximum number of requests received by any server
std::atomic<int> distr[N] = {};
void execute(int number)
{
// Performing iterations
int servers[N]={0};
for(int itr=1; itr<=number; itr++)
{
for(int i=1;i<=N;i++)
{
int index = std::rand()%N;
servers[index]++;
}
int maxRes = -1;
for(int i=0;i<N;i++)
{
maxRes = std::max(maxRes, servers[i]);
servers[i]=0;
}
distr[maxRes] += 1;
}
}
int main(int argc, char const *argv[])
{   
// Initialising
auto start = std::chrono::high_resolution_clock::now();
std::srand(time(NULL));
std::thread t[threads];
for(int i=0;i<threads;i++)
{
t[i] = std::thread(execute, iterations/threads);
}   
for(int i=0;i<threads;i++)
{
t[i].join();
}
for(int i=0;i<=15;i++)
{
double temp = (double)distr[i];
std::cout<<i<<"t"<<distr[i]<<std::endl;
}
auto stop = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start);
std::cout<<duration.count()<<" milliseconds"<<std::endl;
return 0;
}

输出

0   0
1   0
2   0
3   0
4   0
5   0
6   0
7   7
8   201
9   421
10  267
11  68
12  2
13  2
14  4
15  0
1385 milliseconds

虽然我已经多次运行普通代码,每次计数 max = 9> 500,并且没有那么多数据分散,我的意思是只有最大值 = 8,9,10,11 有有效值,其余都是零。

谁能解释一下我做错了什么?

提前感谢!

我没有看到"非常不同的结果",它们只是有些不同,所以看起来有点微妙。我注意到您没有单独播种每个线程 - 这可能与它有关。

PS:如果你想要均匀分布,你不应该使用rand() % N。为什么?请参阅Stephen Lavaveij的解释。正如评论者所建议的那样,当N很小时,偏斜可能很小,但仍然如此。