Created
July 23, 2011 23:56
-
-
Save exallium/1102017 to your computer and use it in GitHub Desktop.
Simple C++0x Thread example
This file contains hidden or 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
// Author: Alex Hart | |
// Compiling: g++ -o hello hello.cpp -lpthread --std=c++0x | |
#include<iostream> | |
#include<thread> | |
#include<cstdlib> | |
using namespace std; | |
mutex a; | |
void th_func(int id) { | |
a.lock(); | |
cout << "Hello World! " << id << endl; | |
a.unlock(); | |
} | |
int main(int argc, char** argv) { | |
thread* th_list[20]; | |
for(int id = 0; id < 20; id++) { | |
th_list[id] = new thread(th_func, id); | |
} | |
for(auto thread : th_list) { | |
thread->join(); | |
} | |
for(auto thread: th_list) { | |
delete thread; | |
} | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment