When Task-Based Programming Is Inappropriate

Using the task scheduler is usually the best approach to threading for performance, but there are cases when the built-in task scheduler is not the most appropriate solution. The task scheduler is intended for high-performance algorithms composed of nonblocking tasks. It still works if the tasks block occasionally. However, if threads block frequently, there is a performance loss when using the task scheduler because the processor is not doing any work while the task is blocked.

Blocking typically occurs while waiting for I/O or mutexes for long periods. If threads hold mutexes for long periods, your code is not likely to perform well anyway, no matter how many threads it has. If you have blocking tasks, it is best to use full-blown threads for those. The task scheduler is designed so that you can safely mix your own threads with Threading Building Blocks tasks.

Get Intel Threading Building Blocks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.