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

How does TBB protect against stack overflows?

Lucian_T_
Beginner
650 Views

Hello

How does Intel TBB protect against stack overflows on the worker threads? In the early versions (and in the Intel TBB book) it was done by checking the depth of the tasks. To my understanding this feature was dropped. What other protection replaces it?

 

Example:

  • let's assume that we have 1000 root-tasks, and each of these root-tasks can go down by 1000 levels (no continuation-style, just stack based spawning, like in the classic Fibonacci example)
  • let's now assume that our tasks consume some stack memory, that we would fit 2000 tasks into a thread tasks, but not more
  • if we have enough stack to execute 1 root-task completely on one thread, we don't have the space to execute 10 root-tasks on the same thread

How does Intel TBB prevents endless stack filling?

 

Thank you very much,

LucTeo

 

0 Kudos
4 Replies
Vladimir_P_1234567890
650 Views

hello, you can increase stack size if needed. AFAIR default thread size is 1 mb for a windows application and 2 mb for tbb workers. but nobody prevents to increase it.

TBB works efficiently with the stack on its high-level algorithms but if you want to program in low level tasks you need to think how much stack you need.

--Vladimir

0 Kudos
Lucian_T_
Beginner
650 Views

Hello Vladimir

 

 

Thank you very much for your answer.

Yes, indeed, I can increase the stack at any time, but I don't have any guarantee that with a fixed stack size I will not overflow given enough tasks to be executed (and a lot of stealing). In my example, I have to budget the stack for 1000*1000 tasks to make sure that I don't overflow. To my assumptions that is a 500 times bigger stack! I cannot simply say: I'll give 500MB for all my working threads,

Isn't there a mechanism already implemented in TBB for limiting the stack growth?

 

0 Kudos
RafSchietekat
Valued Contributor III
650 Views

TBB threads stop stealing when they're halfway through their stack, last time I checked anyway.

0 Kudos
Lucian_T_
Beginner
650 Views

Thank you Raf!

That is what I was looking for. Also found it in the code (search for "my_stealing_threshold" variable).

It's a really neat trick ;)

0 Kudos
Reply