Intel® oneAPI Threading Building Blocks
Ask questions and share information about adding parallelism to your applications when using this threading library.
2465 Discussions

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

damienhocking
New Contributor I
302 Views
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
302 Views
Method execute() of your task has calls set_ref_count(1) but does not have any waiting method called.
0 Kudos
damienhocking
New Contributor I
302 Views
Oh, duh. No child tasks are spawned in that execute, so the ref count is wrong. Thank you.
0 Kudos
Reply