Advertisement
STANAANDREY

asyncFileReader v1.0

Jul 17th, 2021
1,035
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.96 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. string asyncFileReader(string fname, ostream &out = cout) {
  5.     string line, fdata;
  6.     ifstream fin(fname);
  7.     constexpr bool DEMO = true;
  8.     bool finished = false;
  9.     out << "loading";
  10.     auto utilReader = [&]()->void {
  11.         while (getline(fin, line)) {
  12.             fdata += line + '\n';
  13.             if (DEMO) {
  14.                 //simulate a large file
  15.                 for (int i = 0; i < (int)1e8; i++) {
  16.                     continue;
  17.                 }
  18.             }
  19.         }
  20.         finished = true;
  21.         out << endl;
  22.     };
  23.     auto loadingScreen = [&]()->void {
  24.         while (!finished) {
  25.             out << '.';
  26.             this_thread::sleep_for(chrono::milliseconds(100));
  27.         }
  28.     };
  29.     thread t1(utilReader);
  30.     thread t2(loadingScreen);
  31.     t1.join();
  32.     t2.join();//*/
  33.     return fdata;
  34. }
  35.  
  36. int main() {
  37.     puts(asyncFileReader("text.in").c_str());
  38.     return 0;
  39. }
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement