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

spawn with enqueue

Ceyhun
Beginner
479 Views

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

0 Kudos
Ceyhun
Beginner
479 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 :)

0 Kudos
RafSchietekat
Valued Contributor III
479 Views

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

Huh? (as they say...)

 

0 Kudos
Reply