I want to exclude main thread. I have a longrunning work that should run independent of main thread. Also, the longrunning work has to work by splitting it multiple tasks. I have tried to create a root task and enqueue it. Afterwards, the root task spawned the child tasks.
longtask *root_task = new(task::allocate_root())longtask();
set_ref_count(m_task_count + 1);
for(int i = 0; i < m_task_count; ++i)
spawn(*new(allocate_child())task_func(m_func, m_data, i, m_task_count));
Why doesn't it work? It works when I create explicitly worker thread(The worker thread spawns the childs and calls wait_for_all, there is no enqueue). So Main thread or another explicitly created worker thread can spawn childs only?
Did you mean that parent task doesn't wait child tasks' completion, and ready to continue?
It implies that you cannot use spawn with wait_for_all(). You may search detail in TBB's doc by using word "Continuation Passing".
First of all, I thank you very much for your comment. After reading your answer, I examined again the given code. I realized that the problem was in a different place which a multi-thread programming mistake was in. Normally, the code above works. Anyway, I could not have realized this mistake without your answer :)