Skip to content

Instantly share code, notes, and snippets.

@asw456
Forked from amidvidy/threadtest.cpp
Created August 6, 2014 22:24
Show Gist options
  • Save asw456/3e39adef660ebbb1ae34 to your computer and use it in GitHub Desktop.
Save asw456/3e39adef660ebbb1ae34 to your computer and use it in GitHub Desktop.
// compile with g++-4.7 -std=c++11 -pthread threadtest.cpp -o threadtest
#include <iostream>
#include <thread>
#include <cstdlib>
#include <chrono>
static const int num_threads = 10;
int main() {
srand((unsigned)time(nullptr));
std::thread threads[num_threads];
for (int i = 0; i < num_threads; ++i) {
threads[i] = std::thread([=]() {
std::this_thread::sleep_for(std::chrono::milliseconds(rand() % 100));
std::cout << "Launched from thread " << i << std::endl;
});
}
std::cout << "Launched from main" << std::endl;
for (auto &thread : threads) {
thread.join();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment