-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmulti_threading.cpp
More file actions
62 lines (49 loc) · 1.36 KB
/
multi_threading.cpp
File metadata and controls
62 lines (49 loc) · 1.36 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
50
51
52
53
54
55
56
57
58
59
60
61
62
//multi_threading.cpp
#include <iostream>
#include <thread>
#include <mutex>
#include <future>
#include <cstdlib>
#include <array>
#include <vector>
#include <chrono>
#define THREADING_VER
int main(void)
{
using Mutex = std::mutex;
using LockGuard = std::lock_guard<Mutex>;
const auto start = std::chrono::system_clock::now();
std::vector<long> data_base;
#ifdef THREADING_VER
constexpr auto TASK_NUM{10};
Mutex mux;
auto work
{
[&data_base, &mux](void)
{
for (long i = 0; i != 10'000'000; ++i)
{
LockGuard lock{mux};
data_base.emplace_back(i);
}
}
};
std::array<std::future<void>, TASK_NUM> task_list;
for (auto& task : task_list)
{
task = std::async(std::launch::async, work);
}//all task aren't deferred.
for (auto& task : task_list) { task.wait(); }
#else
for (int j = 0; j != TASK_NUM; ++j)
{
for (long i = 0; i != 10'000'000; ++i) { data_base.emplace_back(i); }
}
#endif
std::cout << data_base.size() << std::endl;
std::cout <<
std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now() - start).count()
<< std::endl;
std::system("pause");
return 0;
}