Skip to content

Instantly share code, notes, and snippets.

@dulimarta
Last active January 28, 2020 02:42
Show Gist options
  • Save dulimarta/cb0ec4a5d28cec69d8ec829e9c0d08f5 to your computer and use it in GitHub Desktop.
Save dulimarta/cb0ec4a5d28cec69d8ec829e9c0d08f5 to your computer and use it in GitHub Desktop.
Lab04 - Sample 1 (C++)
// Compile with -lpthread flag
#include <iostream>
#include <thread>
#include <unistd.h>
using namespace std;
void do_greeting();
int main() {
cout << "From PID " << getpid()
<< " thread id " << std::this_thread::get_id() << endl;
// start thread at a function
std::thread one(do_greeting);
one.join(); // join() or detach() is required
return 0;
}
void do_greeting() {
std::this_thread::sleep_for(chrono::seconds(1));
cout << "Thread version of Hello, world. PID=" << getpid()
<< "TID=" << std::this_thread::get_id() << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment