Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <thread>
- #include <time.h>
- #include <windows.h>
- using namespace std;
- void Traspasos();
- void Contabilizar();
- void Proceso1();
- void Proceso2();
- bool bandera_proceso1,bandera_proceso2;
- int turno;
- int x[100];
- int main()
- {
- int i,sum=0;
- for(i=0;i<100;i++){
- x[i]=100;
- sum=sum+x[i];
- }
- cout<<"Dinero en el banco: "<<sum<<endl;
- system("pause");
- thread pro1(Proceso1);
- thread pro2(Proceso2);
- pro1.join();
- pro2.join();
- return 0;
- }
- void Proceso1()
- {
- while (true){
- bandera_proceso1=true;
- turno=1;
- while(bandera_proceso2=true && turno==1){
- Contabilizar();
- }
- bandera_proceso1=false;
- Sleep(1000);
- }
- }
- void Proceso2()
- {
- while(true){
- bandera_proceso2=true;
- turno=0;
- while(bandera_proceso1=true && turno==0){
- Traspasos();
- Sleep(1000);
- }
- bandera_proceso2=false;
- }
- }
- void Traspasos()
- {
- srand(time(NULL));
- int t1,t2,can;
- t1=rand()%99;
- t2=rand()%99;
- can=rand()%9;
- x[t1]=x[t1]+can;
- x[t2]=x[t2]-can;
- }
- void Contabilizar()
- {
- int sum=0,i;
- for(i=0;i<100;i++){
- cout<<"Cuenta "<<(i+1)<<" "<<x[i]<<endl;
- sum=sum+x[i];
- }
- cout<<"Dinero en el banco: "<<sum<<endl;
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement