cpp11/thread2.cc

30 lines
601 B
C++
Raw Normal View History

2012-05-25 09:06:00 -07:00
// from: http://solarianprogrammer.com/2011/12/16/cpp-11-thread-tutorial/
2012-05-05 08:34:15 -07:00
#include <iostream>
#include <thread>
2012-05-25 09:06:00 -07:00
static const int num_threads = 8;
2012-05-05 08:34:15 -07:00
void call_from_thread(int tid) {
std::cout << "Launched by thread " << tid << std::endl;
2012-05-25 09:06:00 -07:00
double x = 1.0;
for(int i = 0; i < 2E9; i++) {
x /= 2;
}
2012-05-05 08:34:15 -07:00
}
int main() {
std::thread t[num_threads];
for (int i = 0; i < num_threads; ++i) {
t[i] = std::thread(call_from_thread, i);
}
std::cout << "Launched from the main\n";
for (int i = 0; i < num_threads; ++i) {
t[i].join();
}
return 0;
}