有什么办法可以同时运行2个循环? [英] any way to run 2 loops at the same time?
问题描述
我想为银行队列创建一个c ++程序.每3分钟就有一个新客户进入该队列.每个客户需要5分钟的服务时间.程序在前30分钟后打印出信息
I want to create a c++ program for a bank queue. Where every 3 minutes a new customer enters the queue. every customer needs 5 minutes for the service. The program print out the information after the first 30 minutes
- 每个客户的到达时间
- 每个客户的离开时间
- 排队有多少客户?
- 当前服务的客户是谁?
我编写了当前代码:
#include <queue>
#include <ctime>
#include <time.h>
#include <conio.h>
#include <windows.h>
#include <iostream>
using namespace std;
int main()
{
queue <int> inq;
queue <int> inservice;
int z= 1;
for(int i=0; i<=9; i++)
{
inq.push(z);
Sleep(180000);
_strtime_s(hold_time);
cout<<"Time of arriving for customer number "<<z<<" is: "<<hold_time<<endl;
z++;
}
do
{
inservice.push(inq.front());
Sleep(300000);
_strtime_s(hold_time);
cout<<"Time of leaving for customer number "<<z<<" is: "<<hold_time<<endl;
inq.pop();
}
while(!inq.empty());
cout<<"number of customers waiting : "<<inq.size()<<endl;
cout<<"Customer number "<<inservice.front()<< " is currently serving"<<endl;
return 0;
}
当前代码逐行执行;在队列循环完成之前,不会将客户转移到服务中. 要调整时间,我必须同时运行两个循环.这样客户就可以在为其他顾客提供服务的同时进入队列
The current code executes line by line; customers will not be moved to service until the queue loop is done. To adjust time, I have to run the two loops at the same time. such that customers enter the queue at the same time other costumers are being served
有什么建议吗?
推荐答案
一种解决此问题的复杂方法的确会使用线程,但是如果您不想这样做,则应该随着时间的流逝而循环,即
A sophisticated way to approach this would indeed use threading, but if you don't want to do that you should instead loop over time, i.e.
for (int minutes = 1; mintues <= 30; minutes++) {
// if minutes modulo 3 do something
// if minutes modulo 5 do something
}
您可以循环播放几秒钟,几微秒之类的时间.
You could loop over seconds, microseconds, whatnot.
这篇关于有什么办法可以同时运行2个循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!