Created
February 9, 2014 18:56
-
-
Save socantre/8904310 to your computer and use it in GitHub Desktop.
break thread pool async implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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