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

calling this_tbb_thread::yield() from within a task

damienhocking
New Contributor I
970 Views
Last year Intel sponsored an article and presentation about optimising game architectures. At one point the article described calling this_tbb_thread::yield() from within a task's execute() method to allow other tasks or threads to decrement an atomic counter as part of synchronized callbacks.

I just want to make absolutely sure: If you have N worker threads running in the task scheduler, calling this_tbb_thread::yield() from within a task temporarily reduces the number of worker threads executing any tasks to N-1, correct?

Damien
0 Kudos
5 Replies
RafSchietekat
Valued Contributor III
970 Views
I wouldn't think so, but I haven't read the article so I don't know about the real intention.
0 Kudos
damienhocking
New Contributor I
970 Views
I only mentioned the article to provide some context. There's not a lot of docs around on how this_tbb_thread::yield() interacts with the task scheduler. I'm wondering what actually happens under the hood when you call that from within a task. If it's truly a thread yield, if called from within a task I would assume that that task's actual thread inside the task scheduler gives up part of it's slice to let other threads run, hence the N-1. If it behaves like a task yield, it could be an interesting way to preempt and order fire-and-forget tasks. Either way, it would be good to know for sure.
0 Kudos
RafSchietekat
Valued Contributor III
970 Views
I see no reason not to assume that it acts directly on the executing thread (note that this_tbb_thread has been deprecated in favour of std::this_thread, which should be clearer in its intention, i.e., nothing specific to TBB). If the machine is currently oversubscribed, another thread probably gets to run instead, otherwise presumably the yield would have no effect. I don't know what you would want to do with it on an enqueued task?
0 Kudos
Alexey-Kukanov
Employee
970 Views
this_thread::yield() in TBB implementation maps to SwitchToThread() on Windows and sched_yield() on UNIX compatible systems. The exact behavior of these calls is OS specific, but generally I understand it the same way as Raf described.
0 Kudos
damienhocking
New Contributor I
970 Views
Thanks gents. As it turns out a couple of my FAF task types sometimes run too long and they get destroyed before they complete, so I need to package them into their own threads anyway. I can synch those by yielding appropriately with callbacks.
0 Kudos
Reply