
hi, I used the following piece of code after following your suggestions. Iam new to boost and even multi-threading When I ran this piece of code on my machine I found that for every run 4 threads are only used out of the pool. As I have 4 cores in my machine. Is it so?? #include<iostream>#include<boost/asio.hpp>#include<boost/thread.hpp>boost::atomic_int thrd_gen(0);voiddowork(inti){thread_local inttid =++thrd_gen;std::cout <<"hello";std::cout <<" thread ID :"<<tid <<"\n";}intmain(){boost::asio::io_service ioservice;boost::thread_group group;boost::optional<boost::asio::io_service::work>work(boost::asio::io_service::work{ioservice});for(size_ti=0;i<boost::thread::hardware_concurrency();i++){group.create_thread(boost::bind(&boost::asio::io_service::run,&ioservice));}std::cout <<"in main thread size="<<group.size()<<std::endl;for(inti =0;i <=5;++i){for(inti=0;i<5;i++){ioservice.post(boost::bind(dowork,10));}boost::this_thread::sleep_for(boost::chrono::milliseconds(600));std::cout <<"waking up\n";}work.reset();// allow threads to exitgroup.join_all();// await proper thread shutdown } Also, my application is time-sensitive and I need to do some time measures. If I use sleep my results will be incorrect. So, please suggest an alternative way to get notified when all the threads finish without using sleep. Thanks for your time on this

On 14/07/2014 17:58, imran sheikh wrote:
When I ran this piece of code on my machine I found that for every run 4 threads are only used out of the pool. As I have 4 cores in my machine. Is it so?? [...] for(size_t i=0; i<boost::thread::hardware_concurrency(); i++) { group.create_thread( boost::bind(&boost::asio::io_service::run, &ioservice) ); }
You're creating 4 threads for your pool. This should not surprise you.
Also, my application is time-sensitive and I need to do some time measures. If I use sleep my results will be incorrect. So, please suggest an alternative way to get notified when all the threads finish without using sleep.
There isn't any obvious reason to call sleep in the code that you had to begin with. If you want to retrieve the individual results of each asynchronous task rather than waiting for all tasks to complete, you might want to read up on futures.

hi, i want to reuse my query is there any way to know that the threads finished the intended work without using sleep?? please suggest any option replace for sleep.thanks On Monday, 14 July 2014 1:52 PM, Gavin Lambert <gavinl@compacsort.com> wrote: On 14/07/2014 17:58, imran sheikh wrote:
When I ran this piece of code on my machine I found that for every run 4 threads are only used out of the pool. As I have 4 cores in my machine. Is it so?? [...] for(size_t i=0; i<boost::thread::hardware_concurrency(); i++) { group.create_thread( boost::bind(&boost::asio::io_service::run, &ioservice) ); }
You're creating 4 threads for your pool. This should not surprise you.
Also, my application is time-sensitive and I need to do some time measures. If I use sleep my results will be incorrect. So, please suggest an alternative way to get notified when all the threads finish without using sleep.
There isn't any obvious reason to call sleep in the code that you had to begin with. If you want to retrieve the individual results of each asynchronous task rather than waiting for all tasks to complete, you might want to read up on futures. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
Gavin Lambert
-
imran sheikh