-
Notifications
You must be signed in to change notification settings - Fork 0
/
64-thread.cc
61 lines (51 loc) · 1.46 KB
/
64-thread.cc
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
//
// Program
// Create multiple async and save respective future to a container.
//
// Compile
// g++ -Wall -Wextra -pedantic -std=c++17 -pthread -o 64-thread 64-thread.cc
//
// Execution
// ./64-thread
//
#include <iostream>
#include <vector>
#include <thread>
#include <chrono>
#include <future>
using namespace std::chrono_literals;
//
// Function to be called for async
//
static void async_callback(int idx) {
auto id = std::this_thread::get_id();
std::cout << "Thread id # " << id << " is having seed # " << idx << '\n';
std::cout << "Thread id # " << id << " is doing job" << '\n';
std::this_thread::sleep_for(3s);
std::cout << "Thread id # " << id << " is complete" << '\n';
}
//
// Entry function
//
int main() {
std::cout << "--- std::async with std::vector ---" << '\n';
constexpr int count = 3;
std::vector <std::future<void>> futures;
for (int idx = 0; idx < count; ++idx) {
// Use emplace_back as std::future is not copyable otherwise
// use std::move to push to container
futures.emplace_back(std::async(std::launch::async, async_callback, idx + 100));
}
std::cout << "main is busy in doing other job..." << '\n';
std::this_thread::sleep_for(2s);
std::cout << "main is waiting/extracting results from async..." << '\n';
for (int idx = 0; idx < count; ++idx) {
if (futures[idx].valid()) {
futures[idx].wait();
}
else {
std::cout << "async id # " << idx << " is invalid" << '\n';
}
}
return 0;
}