normal_distribution
// 将随机数引擎和分布绑定一个函数对象
auto norm = std::bind(nd, re);
// 保存随机数的容器
vector
// 产生随机数
for(int i = 0;i<700;++i)
++mn[round(norm())];
int secs = 100;
// 产生0到9的随机数,表示观众随机地到达某一个检票口
uniform_int_distribution
// 进入检票口队列
for(auto i:mn)
{
cout<
for(auto vi = 1; vi <= i; ++vi)
{
// 将观众添加到某个gate的队列中
(vgates[index(re)]).add(viewer());
// 等待一段时间
int t = round(secs/(float)(i+1));
this_thread::sleep(
posix_time::milliseconds(t));
}
}
// 观众已经全部到达,进入队列
cout<<"finish"< mtx.lock(); finish = true; mtx.unlock(); //cout<<"unlock"< } int main() { int i = 1; // 启动检票线程 for(gate& g:vgates) { g.start(i); ++i; } // 启动到达线程,看看,在C++11中新线程的创建就这么简单 thread arr = thread(arrival); // 等待线程结束 arr.join(); int total = 0; // 等待检票线程结束,并输出处理的人数 for(gate& g:vgates) { g.join(); total += g.getcount(); cout<<"gate "< <<" processed "< } cout<<"there are "< return 0; } 这就是一个线程库的简单应用,模拟了非常复杂的场景。 因为自己对多线程开发还不太熟悉,这个程序在某些特定条件下会产生了死锁,还有待进一步完善 摘自 我的第一本C++书