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

Question about execution of tasks.

morle
Beginner
666 Views

When working with tasks, for example with this:

[cpp]class MyTask: public tbb::task { private: int x; private: void DoSomething(...){...} // Used only inside execute void DoMore(...){...} // Used only inside execute public: MyTasks(...){...} tbb::task* execute(){...} };[/cpp]

Once the task is running, is it possible to execute concurrently DoSomething() or DoMore() by other threads or those methods can only be invoked by the thread that started the execution of the task?.

I read that TBB allows job stealing, but what it steals? Does it steal a piece of data, compute it and return the result to the main thread or could it be possible for the stealing to execute the private methods?

Sorry If my question isn't clear!

Thanks.

0 Kudos
7 Replies
RafSchietekat
Valued Contributor III
666 Views
You can execute DoSomething()/DoMore() in another thread (sounds costly) if they are thread-safe and if you assure that MyTask lives long enough, which may be tricky (the only valid scenario that readily comes to mind involves recycling), so it may not be what you want to do.

As documented, TBB threads may "steal" from each other's task pools when they have exhausted their own.
0 Kudos
morle
Beginner
666 Views
In this scenario no recycling is used.

What I wanted to know if TBB, when stealing work from the pool, could get the piece of work that uses those functions. If such thing is possible...

Thanks.
0 Kudos
jimdempseyatthecove
Honored Contributor III
666 Views
In very loose terms, a task can be thought of as a functor (function address together with arguments). Zero, one or more of the same functor with different or same arguments may be enqueued at any given time.

The thread scheduler binds (assigns) an instance of an enqueued functor (and args) to a thread. That thread alone runs the functor to completion. Note that other threads may be bound (assigned) to run the additional instances of the functor concurrently.

The function call, represented by the functor and args, is run by the assigned thread, which can be thought of as the "master thread" for that instance of the function call. This function may contain additional parallel constructs, which may run concurrently using additional threads, but does not reassign the master thread of function relationship with the function's original starting thread. The original function "master thread" is the thread that completes the function call, represented by the functor and args. When (iif) the programmer creates non-joining thread constructs (e.g. asynchronous threads) within the function call, represented by the functor and args, it is the programmers responsibility to assure the lifetime of the function call is consistent with the expection requirement of the non-joining thread constructs spawned by that function.

Jim Dempsey
0 Kudos
RafSchietekat
Valued Contributor III
666 Views
#2 "In this scenario no recycling is used."
Another scenario would be to execute those operations before the task is spawned, or maybe a high-level task with lots of work in the descendants, but it still seems suspicious to me, because you probably don't want to have a task waiting for an application thread to finish. Try to express everything with tasks instead.

#3 "The function call, represented by the functor and args, is run by the assigned thread, which can be thought of as the "master thread" for that instance of the function call."
Just be careful with the terminology: for TBB, a master thread is any application thread that initiates TBB work, so a task may execute either on a master or on a worker.

0 Kudos
jimdempseyatthecove
Honored Contributor III
666 Views
>>::a master thread:: is any application thread that initiates TBB work

This is why I said:

::can be thought of as the "master thread" for that instance of the function call::

Use the complete context of what is being said.

Jim
0 Kudos
RafSchietekat
Valued Contributor III
666 Views
"Use the complete context of what is being said."
Sure, but the semantic overload (such a "master" could be a TBB "worker") still seemed potentially confusing.
0 Kudos
jimdempseyatthecove
Honored Contributor III
666 Views
A good programmer, such as yourself, must have an understanding of recursion. Recursion not only refers to a programming style, but also into language constructs.

A country has a leader, countries are divided into regions, a region has a leader, region is divided into smaller sections, those sections have a leader, etc...

Each division (recursion) has a leader (analog of a master thread).

The master thread of the application.
The master thread of the TBB thread pool (not necessisarily The Master Thread of the application)
The master thread of a task (not necessisarily The Master Thread of the TBB thread pool)

Jim






0 Kudos
Reply