What's the proper way to start a boost thread for each hardware thread available? I can do this... but it is clunky and does not scale (especially when there are lots of cores): const unsigned int hwt() { const unsigned int t = boost::thread::hardware_concurrency(); return t; } if ( t == 2 ) { boost::thread _01( task, thread_data, St ); boost::thread _02( task, thread_data, St ); _01.join(); _02.join(); } if ( t == 4 ) { boost::thread _01( task, thread_data, St ); boost::thread _02( task, thread_data, St ); boost::thread _03( task, thread_data, St ); boost::thread _04( task, thread_data, St ); _01.join(); _02.join(); _03.join(); _04.join(); } It seems that a for loop (or something similar) could be used to start the same number of boost threads as there are HWTs. Could someone point me in the right direction? Thanks, Brad
On Thu, Jun 9, 2011 at 18:13, Brad
What's the proper way to start a boost thread for each hardware thread available? I can do this... but it is clunky and does not scale (especially when there are lots of cores):
Sound like you want a thread_group, to which you can call create_thread() or add_thread() in a loop then join_all(): http://www.boost.org/doc/html/thread/thread_management.html#thread.thread_ma... ~ Scott Or you could always do silly things with recursion: void foo(int n = boost::thread::hardware_concurrency()) { if (n < 1) return; boost::thread bar( task, thread_data, St ); foo(n-1); bar.join(); }
On Fri, Jun 10, 2011 at 3:13 AM, Brad
What's the proper way to start a boost thread for each hardware thread available? I can do this... but it is clunky and does not scale (especially when there are lots of cores):
const unsigned int hwt() { const unsigned int t = boost::thread::hardware_concurrency(); return t; }
try boost::thread_group [1] boost::thread_group tg; for (int i =0; i < hwt(); ++i) { tg.create_thread(...); } tg.join_all(); Best regards, Christoph [1] http://www.boost.org/doc/libs/1_42_0/doc/html/thread/thread_management.html#...
On Thu, Jun 9, 2011 at 11:21 PM, Christoph Heindl
On Fri, Jun 10, 2011 at 3:13 AM, Brad
wrote: What's the proper way to start a boost thread for each hardware thread available? I can do this... but it is clunky and does not scale (especially when there are lots of cores):
const unsigned int hwt() { const unsigned int t = boost::thread::hardware_concurrency(); return t; }
try boost::thread_group [1]
boost::thread_group tg;
for (int i =0; i < hwt(); ++i) { tg.create_thread(...); } tg.join_all();
Best regards, Christoph
[1] http://www.boost.org/doc/libs/1_42_0/doc/html/thread/thread_management.html#... _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
I'm not sure if this was also part of the question, but even if not, I'm also curious: Is there a boost-ey way of setting the affinity so that the threads are pinned to physical cores? I have my own wrappers at the moment, but I'd love to use something from boost, if available. Thanks, Brian
participants (4)
-
Brad
-
Brian Budge
-
Christoph Heindl
-
Scott McMurray