Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Ceyhun
Beginner
86 Views

spawn with enqueue

Hi,

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.

For example,

longtask *root_task = new(task::allocate_root())longtask();

task::enqueue(*root_task);

class longtask

{

         task* execute()

       {

        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));
        }

        wait_for_all();

       }

}

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?

0 Kudos
3 Replies
Peter_W_Intel
Employee
86 Views

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".

Ceyhun
Beginner
86 Views

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 :)

RafSchietekat
Black Belt
86 Views

"It implies that you cannot use spawn with wait_for_all()."

Huh? (as they say...)

 

Reply