粗体简短解释。
我有一个tbb :: task,可以这样总结:
class UpdateTask
: public tbb::task
{
public:
tbb::task* execute()
{
// some work...
if( can_continue() )
{
// make sure this is re-executed
this->recycle_to_reexecute(); //looks like it is deprecated but it's more clear to me
return nullptr; // ? or this? or tbb::empty_task?
}
return nullptr;
}
};
我希望在完成任务后立即重新安排此任务,直到条件填满。 我用这种方式分配这个任务:
UpdateTask& updater = *new(tbb::task::allocate_root()) UpdateTask();
不确定它与问题有关。
问题:当我运行代码时,我从tbb(最后一个版本,4.1.5)得到断言(在Debug中),我找不到让它正常工作的方法。
我已经重新阅读了文档,但我找不到一个简单的例子,我不清楚我做错了什么。 我做了一些实验:
Assertion t_next failed on line 485 of file ...\itbb\src\tbb\custom_scheduler.h
Detailed description: reexecution requires that method execute() return another
task
Assertion t_next->state()==task::allocated failed on line 452 of file ...\itbb\src\tbb\custom_scheduler.h
Detailed description: if task::execute() returns task, it must be marked as allo
cated
new(tbb::task::allocate_child()) tbb::empty_task()
。我收到了这条消息的断言Assertion p.ref_count==0 failed on line 107 of file ...\itbb\src\tbb\custom_scheduler.h
Detailed description: completion of task caused predecessor's reference count to
underflow
。那么,该怎么做?我认为它很简单但无法找到它的完成方式。 它与任务引用计数有关吗?
更新:这是一个完整的代码,它是我所拥有的良好的近似:
#include <iostream>
#include <atomic>
#include <tbb/tbb.h>
namespace test { class UpdateTask : public tbb::task { public:
UpdateTask() { std::cout << "[UpdateTask]" << std::endl; }
~UpdateTask() { std::cout << "\n[/UpdateTask]" << std::endl; }
tbb::task* execute() { std::cout << "Tick "<< m_count <<std::endl;
++m_count;
// make sure this is re-executed this->recycle_to_reexecute(); //return new(tbb::task::allocate_continuation()) tbb::empty_task(); return nullptr; }
private:
std::atomic<int> m_count;
};
tbb::task_list m_task_list;
void register_update_task() { UpdateTask& updater =
*new(tbb::task::allocate_root()) UpdateTask(); m_task_list.push_back( updater ); }
void run_and_wait() { tbb::task::spawn_root_and_wait( m_task_list ); }
void tbb_test() { register_update_task(); run_and_wait();
}
}
int main()
{
test::tbb_test();
std::cout << "\nEND";
std::cin.ignore();
return 0;
}
所以,在这里我得到了第一个例外,说我应该返回一个任务。在execute()
函数中,如果我用评论的那个替换返回,那么它似乎有效,但是有两个问题:
我必须得出结论,这也不是正确的方法。
答案 0 :(得分:3)
this->recycle_to_reexecute();
已弃用。
替换为:
this->increment_ref_count();
this->recycle_as_safe_continuation();
享受
P.S。:当然结束(在您的情况下)从执行返回NULL。