Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
2 changed files
with
43 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// MThread.cpp : Defines the entry point for the console application. | ||
// | ||
|
||
#include "stdafx.h" | ||
#include <iostream> | ||
#include <thread> | ||
|
||
static const int num_threads = 10; | ||
|
||
void call_from_thread() { | ||
std::cout << "Hello from thread" << std::endl; | ||
} | ||
|
||
int main() | ||
{ | ||
//Launch a thread | ||
std::thread t1(call_from_thread); | ||
//Join the thread with the main thread | ||
t1.join(); | ||
|
||
getchar(); | ||
|
||
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); | ||
} | ||
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(); | ||
} | ||
|
||
getchar(); | ||
return 0; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters