Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- string asyncFileReader(string fname, ostream &out = cout) {
- string line, fdata;
- ifstream fin(fname);
- constexpr bool DEMO = true;
- bool finished = false;
- out << "loading";
- auto utilReader = [&]()->void {
- while (getline(fin, line)) {
- fdata += line + '\n';
- if (DEMO) {
- //simulate a large file
- for (int i = 0; i < (int)1e8; i++) {
- continue;
- }
- }
- }
- finished = true;
- out << endl;
- };
- auto loadingScreen = [&]()->void {
- while (!finished) {
- out << '.';
- this_thread::sleep_for(chrono::milliseconds(100));
- }
- };
- thread t1(utilReader);
- thread t2(loadingScreen);
- t1.join();
- t2.join();//*/
- return fdata;
- }
- int main() {
- puts(asyncFileReader("text.in").c_str());
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement