- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread.cpp
More file actions
Latest commit
59 lines (45 loc) · 1.4 KB
/
Copy paththread.cpp
File metadata and controls
59 lines (45 loc) · 1.4 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
/* Notes:
*
* Thread:
*
* 1. In every application there is a default thread which is main(),
in side this we create other threads.
*
* 2. A thread is also known as lightweight process.
Idea is achieve parallelism b dividing a process into multiple threads.
*
* For example:
* (a) The brower has multiple tabs that can be different threads.
* (b) MS Word must be using multiple threads, one thread to format the text,
another thread to process inputs (spell checker).
* (c) VS Code's Intellicence
*
* WAYS TO CREATE THREADS IN C++11
1. Function Pointers
2. Lambda Functions
3. Functors
4. Member Functions
5. Static Member functions
*/
#include<iostream>
#include<thread>
usingnamespacestd::literals::chrono_literals;
staticbool s_Finished = false;
voidDoSomething() {
std::cout << "Do Somehting() --- Thread ID = " << std::this_thread::get_id() << "\n\n";
while (!s_Finished) {
std::cout << "Do something......\n";
std::this_thread::sleep_for(1s);
}
}
intmain()
{
std::thread worker(DoSomething); // here, worker thread takes a function pointer**
// Do some work below while DoSomething() thread is running.....
std::cin.get(); // wait for user to press enter
s_Finished = true;
worker.join(); // wait for DoSomething() thread to finish it's work
// ---------------- thread finished
std::cout << "main() --- Thread ID = " << std::this_thread::get_id() << std::endl;
std::cin.get();
}