Skip to content

Instantly share code, notes, and snippets.

@smamran
Created October 6, 2015 02:43
Show Gist options
  • Select an option

  • Save smamran/a43046eeb00019aa7685 to your computer and use it in GitHub Desktop.

Select an option

Save smamran/a43046eeb00019aa7685 to your computer and use it in GitHub Desktop.
STD C++ Thread
#include <iostream> // std::cout
#include <thread> // std::thread
void foo()
{
for (int i = 0; i < 1500000000; i++);
}
void bar(int x)
{
for (int i = 0; i < 1500000000; i++);
}
using namespace std;
int main()
{
std::thread first(foo); // spawn new thread that calls foo()
std::thread second(bar, 0); // spawn new thread that calls bar(0)
std::thread third(foo);
std::thread fourth(foo);
std::cout << "main, foo and bar now execute concurrently...\n";
// synchronize threads:
first.join(); // pauses until first finishes
second.join(); // pauses until second finishes
third.join();
fourth.join();
std::cout << "foo and bar completed.\n";
cout << "Done in " << clock() * 1000 / CLOCKS_PER_SEC << endl;
getchar();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment