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

hardware concurrency

renorm
Beginner
999 Views
Another quick question.
What is the best way to get the max number of physical threads, thread::hardware_concurrency or task_scheduler_init::default_num_threads? Do these static methods return the same number?

Just curios, why the first one returns unsigned, but the second one returns int?

Thanks in advance,
renorm.
0 Kudos
1 Reply
Andrey_Marochko
New Contributor III
999 Views
Quoting renorm
What is the best way to get the max number of physical threads, thread::hardware_concurrency or task_scheduler_init::default_num_threads? Do these static methods return the same number?

Just curios, why the first one returns unsigned, but the second one returns int?

The latetr one was part of the initial TBB release that did not have any thread abstraction class. It returns int to make it compatible with argument types of other methods, which sometimes use special values (like -1 or -2). I guess it were possible to make them all unsigned, but doing so would've reqiured too many explicit casts between signed/unsigned in the code that uses deltas to increment/decrement the number of active threads. And thus it was decided to leave it int.

When later we introduced class tbb::tbb_thread that has become std::thread (though still available via tbb:: namespace) by now, we made it as close to the C++0x draft as possible. And the draft in particular requires method std::thread::hardware_concurrency to return unsigned.

Thus we maintain both methods for compatibility reasons, and they return the same value.
0 Kudos
Reply