Created
May 6, 2016 19:52
-
-
Save Mikle-Bond/8ac3f22d720a70483e59d5fe1525f479 to your computer and use it in GitHub Desktop.
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
#include <thread> | |
#include <iostream> | |
#if 0 | |
void thread_func() | |
{ | |
std::cout << "Hello, parallel world!" << std::endl; | |
} | |
#else | |
class thread_hold | |
{ | |
int val; | |
public: | |
thread_hold(int i) : val(i) {} | |
void operator() () { std::cout << "Hello, World " << val << std::endl; } | |
}; | |
#endif | |
void daem() | |
{ | |
int i = 0; | |
while (1) i++; | |
} | |
#define join_m(name) \ | |
if (name.joinable()) name.join() | |
#define detach_m(name) \ | |
if (name.joinable()) name.detach() | |
int main() | |
{ | |
std::thread th1(thread_hold(1)); | |
std::thread th2(thread_hold(2)); | |
std::thread th3(thread_hold(3)); | |
std::thread th4(thread_hold(4)); | |
std::thread daemon(daem); | |
join_m(th1); | |
join_m(th3); | |
join_m(th2); | |
join_m(th4); | |
detach_m(daemon); | |
int i = 100; | |
while (i--); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment