Skip to content

Instantly share code, notes, and snippets.

@exallium
Created July 23, 2011 23:56
Show Gist options
  • Save exallium/1102017 to your computer and use it in GitHub Desktop.
Save exallium/1102017 to your computer and use it in GitHub Desktop.
Simple C++0x Thread example
// 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