Community
cancel
Showing results for 
Search instead for 
Did you mean: 
damienhocking
New Contributor I
58 Views

really simple spawn_root_and_wait example causes "Task still has children..." assertion

I'm trying to set up my app where main() just spawns the root task, and then use that root task's execute to set up and drive the rest of the app. My very simple example is throwing an assertion on the number of child tasks. For something this simple, it has to be something I'm doing. Here's the simplest example I can construct that causes the assertion:

#include

#ifndef __TBB_task_scheduler_init_H
#include "tbb/task_scheduler_init.h"
#endif

#ifndef __TBB_task_H
#include "tbb/task.h"
#endif

#ifndef __TBB_tbb_thread_H
#include "tbb/tbb_thread.h"
#endif

using namespace tbb;


class MainAppTask : public task
{
public:
MainAppTask()
{
}
~MainAppTask()
{
}

task* execute()
{ // Overrides virtual function task::execute
set_ref_count(1);
std::cout << "\\n Hello from the root task";
double test = 0.0;
for (int i = 0; i < 1000000; i++)
{
test += i;
}
std::cout << "\\n Test is " << test;
return NULL;
}

};

int main()
{
//tbb::task_scheduler_init init(1);
tbb::task_scheduler_init anonymous;
MainAppTask& a = *new(task::allocate_root()) MainAppTask();
task::spawn_root_and_wait(a);

return 1;
}

The assertion appears after execute starts but before it's finished.

Damien
0 Kudos
2 Replies
Andrey_Marochko
New Contributor III
58 Views

Method execute() of your task has calls set_ref_count(1) but does not have any waiting method called.
damienhocking
New Contributor I
58 Views

Oh, duh. No child tasks are spawned in that execute, so the ref count is wrong. Thank you.
Reply