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

Migration from pthread to TBB

I have developed network based application by using the pthread library. Now, to best utilize the Multi-core platform, I want to give a trial to TBB library. I have just started to get in depth of TBB. In my application there is combination of functional and data decomposition. I am unable to get(understand) the TBB API, which can be used to model the functional decomposition.
Let say, application has three independent functional unit :
  1. FUN1()
  2. FUN2()
  3. and FUN3()
Generally in case of pthread, we call pthread_create() and pass the FUN1, FUN2 and FUN3 as an argument to start these three thread independently.
Now, to do the same thing using TBB, how we should start these task independently(There is no data dependency among these tasks).

0 Kudos
2 Replies
New Contributor III
Well, this is pretty straighforward (as long as you do not mind a bit of C++ class writing and placement-new callingsmiley [:-)], tutorial has easy samples for all this stuff).

1) You create 3 classes derived from tbb::task. Their methods execute() should invoke FUN1, FUN2, and FUN3 correspondingly.
2) Then you allocate instances of these tasks by means of tbb::allocate_root() functions,
3) and place them into tbb::task_list container.
4) At last you call tbb::spawn_root_and_wait() passing it the task list (that now contains 3 instances of your tasks).

The call to tbb::spawn_root_and_wait() will execute your tasks in parallel (depending on the number of available threads), and return when the last of them finishes. Note that this simple form of functional parallelism usually results in significant imbalance (which is really bad as you certainly know). But if your FUNX functions have a potential for data parallelizm, then you'll be all right. You can safely use TBB data parallel algorithms (like parallel_for, parallel_do or pipeline) from inside other TBB tasks. This does not incur any additional overhead, while allows you to achieve great load balance (that is maximally engages all the available computational resourses in doing the work for you).

And do not forget to initialize TBB (by creating an instance of tbb_scheduler_init class) in the main function of your app before you do what is described above.

0 Kudos

Also see my blog There's a small piece of code there that you can copy and paste, which provides aconvenient task_group interface.

0 Kudos