added two threading examples
This commit is contained in:
parent
318f0a9664
commit
d0fdff0b62
18
thread.cc
Normal file
18
thread.cc
Normal file
@ -0,0 +1,18 @@
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
|
||||
//This function will be called from a thread
|
||||
|
||||
void call_from_thread() {
|
||||
std::cout << "Hello, World" << std::endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
//Launch a thread
|
||||
std::thread t1(call_from_thread);
|
||||
|
||||
//Join the thread with the main thread
|
||||
t1.join();
|
||||
|
||||
return 0;
|
||||
}
|
28
thread2.cc
Normal file
28
thread2.cc
Normal file
@ -0,0 +1,28 @@
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
|
||||
static const int num_threads = 10;
|
||||
|
||||
//This function will be called from a thread
|
||||
|
||||
void call_from_thread(int tid) {
|
||||
std::cout << "Launched by thread " << tid << std::endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::thread t[num_threads];
|
||||
|
||||
//Launch a group of threads
|
||||
for (int i = 0; i < num_threads; ++i) {
|
||||
t[i] = std::thread(call_from_thread, i);
|
||||
}
|
||||
|
||||
std::cout << "Launched from the main\n";
|
||||
|
||||
//Join the threads with the main thread
|
||||
for (int i = 0; i < num_threads; ++i) {
|
||||
t[i].join();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user