Skip to content

Instantly share code, notes, and snippets.

@socantre
Created February 9, 2014 18:56
Show Gist options
  • Save socantre/8904310 to your computer and use it in GitHub Desktop.
Save socantre/8904310 to your computer and use it in GitHub Desktop.
break thread pool async implementation
#include <iostream>
#include <future>
#include <vector>
#include <cassert>
struct S {
bool on_main_thread = false;
int tasks_run_on_this_thread = 0;
S() { std::cerr << "S ctor\n"; }
~S() { std::cerr << "S dtor\n"; }
};
thread_local S s;
int main() {
std::vector<std::future<void> > v;
const int task_count = 10;
for (int i = 0; i < task_count; ++i) {
v.emplace_back(std::async([]{
assert(s.tasks_run_on_this_thread == 0 || s.on_main_thread);
++s.tasks_run_on_this_thread;
}));
}
s.on_main_thread = true;
for (auto &&t : v) {
t.get();
}
std::cout << "Tasks run asynchronously: "
<< task_count - s.tasks_run_on_this_thread
<< " (Should be this many + 1 ctors/dtors run)\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment