-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimultaneous_works_on_disk_and_cpu.cpp
More file actions
49 lines (39 loc) · 1.08 KB
/
simultaneous_works_on_disk_and_cpu.cpp
File metadata and controls
49 lines (39 loc) · 1.08 KB
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
//myMain.cpp
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <future>
#include <chrono>
#define MULTITHREAD_VER
int main(void)
{
using namespace std::chrono;
auto start_t{high_resolution_clock::now()};
#ifdef MULTITHREAD_VER
auto write
{
std::async
(
std::launch::async,
[](void)
{
std::ofstream ofs{"output", std::ios_base::trunc};
for (long i = 0; i != 1'000'000L; ++i) { ofs << i; }
}
)
}; //this task won't be deferred.
for (volatile long i = 0; i != 100'000'000L; ++i); //Do other heavy works.
try { write.get(); } //Wait until the writing procedure ends.
catch (const std::exception& _e) { std::cerr << _e.what() << std::endl; }
#else
try
{
std::ofstream ofs{"output", std::ios_base::trunc};
for (long i = 0; i != 1'000'000L; ++i) { ofs << i; }
for (volatile long i = 0; i != 100'000'000L; ++i); //Do other heavy works.
}
catch (const std::exception& _e) { std::cerr << _e.what() << std::endl;}
#endif
std::cout << duration_cast<microseconds>(high_resolution_clock::now() - start_t).count() << std::endl;
std::system("pause");
}