-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSimpleThreading_01.cpp
More file actions
79 lines (54 loc) · 1.9 KB
/
SimpleThreading_01.cpp
File metadata and controls
79 lines (54 loc) · 1.9 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// ===========================================================================
// Simple Threading Demo (std::thread basics) // SimpleThreading_01.cpp
// ===========================================================================
#include <iostream>
#include <thread>
#include <chrono>
#include "../Logger/Logger.h"
namespace SimpleThreading01 {
/*
* std::thread basics
*/
constexpr size_t NumIterations{ 5 };
static void function(int value) {
std::thread::id tid{ std::this_thread::get_id() };
Logger::log(std::cout, "tid: ", tid);
for (size_t i{}; i != NumIterations; ++i) {
Logger::log(std::cout, "in thread ", value);
std::this_thread::sleep_for(std::chrono::seconds{ 1 });
}
Logger::log(std::cout, "Done Thread.");
}
static void test_01() {
Logger::log(std::cout, "Begin");
std::thread::id mainTID{ std::this_thread::get_id() };
Logger::log(std::cout, "main: ", mainTID);
std::thread t1{ function, 1 };
std::thread t2{ function, 2 };
t1.join();
t2.join();
Logger::log(std::cout, "Done.");
}
static void test_02() {
Logger::log(std::cout, "Begin");
std::thread::id mainTID{ std::this_thread::get_id() };
Logger::log(std::cout, "main: ", mainTID);
std::thread t1{ function, 1 };
std::thread t2{ function, 2 };
t1.detach();
t2.detach();
Logger::log(std::cout, "Done.");
using namespace std::chrono_literals;
std::this_thread::sleep_for(6s);
Logger::log(std::cout, "Done Again.");
}
}
void test_simple_threading_01()
{
using namespace SimpleThreading01;
test_01();
test_02();
}
// ===========================================================================
// End-of-File
// ===========================================================================