回收父母作为孩子在那里

Recycling parent as child in tbb

本文关键字:孩子 在那里 父母      更新时间:2023-10-16

我想使用tbb::task s并行化一个看似简单的问题。我的任务可以分成子任务,子任务的数量不能选择,而是由任务的状态决定(事先不知道)。由于父任务不需要其子任务的结果,所以我想将父任务作为子任务回收。我在在线文档或示例中找不到一个很好的工作示例,因此我在这里提出问题。我现在的想法是这样写:

struct my_task : tbb::task {
  typedef implementation_defined task_data;
  task_data DATA;
  my_task(task_data const&data) : DATA(data) {}
  void reset_state(task_data const&data) { DATA=data; }
  bool is_small() const;
  void serial_execution();
  bool has_more_sub_tasks() const;
  task_data parameters_for_next_sub_task();
  tbb::task*execute()
  {
    if(is_small()) {
      serial_execution();
      return nullptr;
    }
    tbb::empty_task&Continuation = allocate_continuation();     // <-- correct?
    task_data first_sub_task = parameters_for_next_sub_task();
    int sub_task_counter = 1;
    tbb::task_list further_sub_tasks;
    for(; has_more_sub_tasks(); ++sub_task_counter)
        further_sub_tasks.push_back(*new(Continuation.allocate_child())
                                     my_task(parameters_for_next_sub_task());
    Continuation.set_ref_count(sub_task_counter);               // <-- correct?
    spawn(further_sub_tasks);
    recycle_as_child_of(Continuation);                          // <-- correct?
    reset_state(first_sub_task);                                // change state
    return this;                                                // <-- correct?
  }
};
my_task*root_task = new(tbb::task::allocate_root())
                    my_task(parameters_for_root_task());
tbb::task::spawn_root_and_wait(*root_task);

这是正确的和/或最好的方法吗?(注意,在上面的代码中,既不生成也不返回空continuation任务)

创建延续的那一行应该是:

tbb::empty_task&Continuation = *new(allocate_continuation()) tbb::empty_task;

set_ref_count和reset_state之间的逻辑看起来是正确的。