In Intel's threading block infrastructure, how to ensure that all threads are not busy waiting for other threads to complete.
Consider, for example, the following code,
#include <tbb/tbb.h>
#include <vector>
#include <cstdlib>
#include <future>
#include <iostream>
std::future<bool> run_something(std::function<bool(bool)> func, bool b) {
auto task = std::make_shared<std::packaged_task<bool()> >(std::bind(func, b));
std::future<bool> res = task->get_future();
tbb::task_group g;
g.run([task]() { (*task)(); });
return res;
};
int main() {
tbb::parallel_for(0, 100, 1, [=](size_t i) {
g.run([] () {
std::cout << "A" << std::endl;
run_something([] (bool b) { return b; }, true).get();
});
});
return EXIT_SUCCESS;
}
Here the function mainappears as tasks, since the TBB library is used in the thread pool. Then, when the second call to create new tasks occurs in the function run_something, the TBB scheduler sees that the threads are not available and are simply deadlocked. That is, I see that this print statement runs exactly 4 times on a machine with four hyperthreads and 8 times on a machine with 8 machines.
, , , task_group task_arena parallel_for ?