Skip to content

Instantly share code, notes, and snippets.

@asit-dhal
Created August 19, 2016 14:30
Show Gist options
  • Save asit-dhal/9a340a40988f79264c3686a90d2d80e3 to your computer and use it in GitHub Desktop.
Save asit-dhal/9a340a40988f79264c3686a90d2d80e3 to your computer and use it in GitHub Desktop.
Gist created by fiddle.jyt.io
// "Copyright [2016] <Asit Dhal, dhal.asitk@gmail.com>"
#include <thread>
#include <iostream>
#include <chrono>
#include <string>
#include <sstream>
#include <functional>
#include <algorithm>
#include <vector>
// returns thread id in std::string
std::string get_str_id() {
auto myid = std::this_thread::get_id();
std::stringstream ss;
ss << "[id=" << myid << "] ";
return ss.str();
}
class worker {
public:
explicit worker(std::string name):name(name) {
thd = std::thread(&worker::execute, this);
}
void execute() const {
std::cout << get_str_id() << name << std::endl;
heavy_work();
}
void heavy_work() const {
std::cout << get_str_id() << "in heavy work" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(2));
}
void join() {
thd.join();
}
private:
std::thread thd;
std::string name;
};
void do_work(int id) {
std::cout << get_str_id() << "in do work, id=" << id << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(2));
}
int main() {
std::cout << get_str_id() << "Main thread " << std::endl;
std::vector<std::thread> threads;
for (int i=0; i < 3; i++) {
threads.push_back(std::thread(do_work, i));
}
std::for_each(threads.begin(), threads.end(),
std::mem_fn(&std::thread::join));
std::vector<worker> workers;
for (int i=0; i < 3; i++) {
workers.push_back(worker(std::string("thread:") + std::to_string(i)));
}
std::for_each(workers.begin(), workers.end(),
std::mem_fn(&worker::join));
std::cout << "main ended" << std::endl;
return 0;
}
auto result = main();
// Press the "play" button to load this file and interact in the terminal.
// As C++ does not allow to redefine symbols, pressing "play" multiple times might cause errors.
// You can restart the whole enviroment by pressing the "reload" button.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment