Thread Blocking: Deadlock, because all threads consumed

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 ?

+4
1

std::future parallelism TBB. std::future::get() let_me_block_in_system_wait_here(). , TBB , TBB, TBB. , TBB TBB.

parallelism , , . tbb::task::enqueue() -.

, g.run() main() g. task_group wait(), : Requires: Method wait must be called before destroying a task_group, otherwise the destructor throws an exception.

. , TBB . task_arena. , .

+2

Source: https://habr.com/ru/post/1599463/


All Articles