Skip to content
Snippets Groups Projects
Commit 17e9aa3a authored by Erik Strand's avatar Erik Strand
Browse files

Add some more basic examples to thread.cpp

parent a3eb5dd6
No related branches found
No related tags found
No related merge requests found
#include <thread>
#include <iostream>
void thread_method() {
std::cout << "hello thread" << std::endl;
void thread_method(int i) {
std::cout << "hello thread " << i << std::endl;
}
struct ThreadObject {
void operator()(int i) const {
std::cout << "hello object " << i << std::endl;
}
};
int main() {
std::thread thread(thread_method);
thread.join();
// Note that no care is taken to avoid race conditions around the global object std::cout.
// Arguments are copied into the child threads.
auto function_thread = std::thread(thread_method, 1);
auto object_thread = std::thread(ThreadObject(), 2);
auto lambda_thread = std::thread([](int i) {
std::cout << "hello lambda " << i << std::endl;
}, 3);
// Without these, the main thread could terminate before the children complete.
function_thread.join();
object_thread.join();
lambda_thread.join();
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment