1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| #include <iostream>
#include <thread>
#include <windows.h>
#include <fstream>
#include <string>
#include <sstream>
#include <mutex>
using namespace std;
int main()
{ mutex lock;
string line, checker;
ostringstream data;
int h=0,m=0,level=0;
float s=0;
fstream myfile;
if (myfile.is_open()) myfile.close();
myfile.open("data.txt",ios::out | ios::in | ios::trunc);
thread t1 ([&h,&s,&m,&myfile,&data,&checker,&level](){
while(1)
{s+=0.1;
if (s==60)
{s=0;m+=1;}
if (m==60)
{m=0;h+=1;}
if (myfile.is_open()){
data<<h<<" "<<m<<" "<<s<<endl;
checker=data.str();
if(checker.find("\n")) level+=1;
}
Sleep(100);
}
});
thread t2 ([&h,&m,&s,&myfile,&line,&level,&lock](){
while(1){
if (myfile.is_open()&& level>0){
lock.lock();
myfile.seekg(0,myfile.beg);
getline(myfile,line);
lock.unlock();
cout<<line;
level-=1;
}
}
});
t1.join();
t2.join();
myfile.close();
return 0;
} |
Partager