C++中多线程的混乱
Confusion in Multithreading in C++
我正在尝试模拟一个概率问题,其中有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
很小时,偏斜可能很小,但仍然如此。
相关文章:
- 在C++中使用cURL和多线程
- 多线程双缓冲区
- 为什么我的多线程作业队列崩溃
- 在main()之外初始化std::vector会导致性能下降(多线程)
- 试图创建一个多线程程序来查找0-100000000之间的总素数
- 为什么一个向量上的多线程操作很慢
- 学习多线程C++:添加线程不会使执行速度更快,即使它看起来应该
- 全局变量 多读取器 一个写入器多线程安全?
- boost::文件系统::recursive_directory_iterator多线程安全
- 如何阻止TensorFlow的多线程
- 如何在多线程中正确使用unique_ptr进行多态性?
- 并发/多线程:是否可以以这种方式生成相同的输出?
- sigwait() 在多线程程序中不起作用
- 多线程程序中出现意外的内存泄漏
- 静态 constexpr 类成员变量对多线程读取是否安全?
- 多线程比没有线程C++慢
- 具有 C++11 多线程的特征库
- 通过安装信号处理程序关闭多线程应用程序
- C++中多线程的混乱
- 多线程文本输出打印到dos控制台重叠/混乱输出