Skip to content

Instantly share code, notes, and snippets.

@dulimarta
Last active January 30, 2020 17:54
Show Gist options
  • Save dulimarta/71a67035a895683258ae6ce08b02507c to your computer and use it in GitHub Desktop.
Save dulimarta/71a67035a895683258ae6ce08b02507c to your computer and use it in GitHub Desktop.
CS452 Lab04 - Sample 2 (C++)
// Compile with -lpthread flag
#include <thread>
#include <iostream>
#include <sys/types.h>
#include <unistd.h>
using namespace std;
/* Thread functions in C++ can take any number of args of any type */
void do_greeting2 (int, string);
int main()
{
srand(getpid());
thread t1(do_greeting2, 30, "First task");
// Or uSe lambda expression
thread t2([] {
do_greeting2( 50, "Second task");
});
t1.join();
t2.join();
return 0;
}
void do_greeting2 (int num_iter, string text)
{
int val = rand() % 2;
cout << "From " << this_thread::get_id() << " " << text << endl;
for (int loop = 0; loop < num_iter; loop++) {
//sleep (1);
if (!val)
cout << loop << " Hello "; // Intentionally no newline
else
cout << loop << " World\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment