Skip to content

Instantly share code, notes, and snippets.

@dmitru
Last active December 19, 2015 04:49
Show Gist options
  • Save dmitru/5900321 to your computer and use it in GitHub Desktop.
Save dmitru/5900321 to your computer and use it in GitHub Desktop.
My learning of concurrent programming in C++11
#include <iostream>
#include <thread>
void hello()
{
std::cout << "HW!" << std::endl;
}
int main()
{
std::thread t(hello);
t.join();
}
/* One thread prints an incrementing counter each second,
* while the second thread is waiting for user to enter
* anything to terminate the program */
#include <unistd.h>
#include <string>
#include <chrono>
#include <thread>
#include <iostream>
class Counter {
private:
int counter;
public:
Counter() : counter(0) { }
void operator() () {
std::cout << counter++ << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
};
int main()
{
Counter counter;
std::thread counterThread(counter);
counterThread.detach();
std::string userInput;
while (std::cin >> userInput)
;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment