-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultithreadingExample.cpp
More file actions
42 lines (33 loc) · 908 Bytes
/
MultithreadingExample.cpp
File metadata and controls
42 lines (33 loc) · 908 Bytes
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
//
// Created by andrey on 4/27/20.
//
#include <iostream>
#include <vector>
#include "MultithreadingExample.h"
#include "future"
#include "Logger.h"
void MultithreadingExample::run() {
Logger logger;
logger.info("start");
auto f1 = std::async(std::launch::async, [&logger]() {
std::this_thread::sleep_for(std::chrono::seconds(1));
logger.info("1 sec action complete");
});
auto f2 = std::async(std::launch::async, [&logger]() {
std::this_thread::sleep_for(std::chrono::seconds(3));
logger.info("3 sec action complete");
});
// 1st option:
// f1.get();
// f2.get();
// 2nd option:
std::vector<std::future<void>> vector;
vector.push_back(move(f1));
vector.push_back(move(f2));
for (const auto &future : vector) {
if (future.valid()) {
future.wait();
}
}
logger.info("end");
}