On Saturday, 12 July 2014 11:10 AM, "boost-users-owner@lists.boost.org" <boost-users-owner@lists.boost.org> wrote: ----- Forwarded Message ----- To post messages to the Boost Users mailing list, you will need to subscribe this e-mail address via the Boost mailing list web page at: http://lists.boost.org/mailman/listinfo.cgi/boost-users You will need to take this step even if you intend to post only through GMane or some other news gateway. In this case, you may wish to edit your subscription options to turn off mail delivery. On Friday, 11 July 2014 11:00 PM, imran sheikh <s.imran0508@yahoo.com> wrote: hi all, i am using boost::asio:: ioservice for the multi-threading in c++. i got stuck ...please see and help. code is like this, discription:
i am creating threadpool using ioservice. creating threads 100. in while (which is 5 times)loop at a time i want to post(posting at do work) 5 threads.
then i do work reset and like this.. i print thread-id also
problems::
threadpool size is 100. but at a posting time it didn't taking the 5 threads..when i printed a id its same for 5 thread so its not executing in parallel..why? i want to post five threads at a time(different threads means thread id is different must)..from threapool..and like this for whole while loop rotation..??????????????????????????
void dowork(int i) { std::cout<<"hello"<<std::endl; cout<<" thread ID :"<<boost::this_thread::get_id(); } int main() { int ch; int i; boost::asio::io_service ioservice; boost::thread_group threadpool; auto_ptr<boost::asio::io_service::work> work(new boost::asio::io_service::work(ioservice)); for(i=0;i<100;i++) { threadpool.create_thread( boost::bind(&boost::asio::io_service::run, &ioservice)); } ch=0; while(ch <= 5) { ch++; cout<<"in main"<<boost::this_thread::get_id()<<endl; for(i=0;i<5;i++) { ioservice.post(boost::bind(dowork,10)); } std::cout<<"size="<<threadpool.size()<<std::endl; work.reset(); ioservice.reset(); ioservice.run(); } } please give me suggestion.i got stuck with it.
On Sat, Jul 12, 2014 at 02:24:48PM +0800, imran sheikh wrote: [snipped tons of empty lines and citation garbage]
threadpool size is 100. but at a posting time it didn't taking the 5 threads..when i printed a id its same for 5 thread so its not executing in parallel..why? i want to post five threads at a time(different threads means thread id is different must)..from threapool..and like this for whole while loop rotation..??????????????????????????
void dowork(int i) { std::cout<<"hello"<<std::endl; cout<<" thread ID :"<<boost::this_thread::get_id(); }
int main() { int ch; int i; boost::asio::io_service ioservice; boost::thread_group threadpool; auto_ptr<boost::asio::io_service::work> work(new boost::asio::io_service::work(ioservice)); for(i=0;i<100;i++) { threadpool.create_thread( boost::bind(&boost::asio::io_service::run, &ioservice)); } ch=0; while(ch <= 5) { ch++; cout<<"in main"<<boost::this_thread::get_id()<<endl; for(i=0;i<5;i++) { ioservice.post(boost::bind(dowork,10)); } std::cout<<"size="<<threadpool.size()<<std::endl; work.reset(); ioservice.reset(); ioservice.run(); } }
please give me suggestion.i got stuck with it.
You are indeed correctly starting 100 threads each run()ing the io_service. However, io_service.reset() in each of the iterations in the while loop completely destroys that. You're then run()ing in the main thread, which blocks until all pending operations are completed. You shall only run() on the threads where you want to block and dispatch handlers. You shall only reset() when you really mean it. Strongly reconsider your logic in the main thread and find out what the functions you call actually do. Also, your posting contains A LOT of whitespace and even repeats the message several times. Look into if your mail client is broken. -- Lars Viklund | zao@acc.umu.se
hi , but if i am not using ioservice.reset() in the while loop then second time the threads are not executing properly. so please give me a strong running solution .i need immediately this one. thanks, imran On Sunday, 13 July 2014 5:20 AM, Lars Viklund <zao@acc.umu.se> wrote: On Sat, Jul 12, 2014 at 02:24:48PM +0800, imran sheikh wrote: [snipped tons of empty lines and citation garbage]
threadpool size is 100. but at a posting time it didn't taking the 5 threads..when i printed a id its same for 5 thread so its not executing in parallel..why? i want to post five threads at a time(different threads means thread id is different must)..from threapool..and like this for whole while loop rotation..??????????????????????????
void dowork(int i) { std::cout<<"hello"<<std::endl; cout<<" thread ID :"<<boost::this_thread::get_id(); }
int main() { int ch; int i; boost::asio::io_service ioservice; boost::thread_group threadpool; auto_ptr<boost::asio::io_service::work> work(new boost::asio::io_service::work(ioservice)); for(i=0;i<100;i++) { threadpool.create_thread( boost::bind(&boost::asio::io_service::run, &ioservice)); } ch=0; while(ch <= 5) { ch++; cout<<"in main"<<boost::this_thread::get_id()<<endl; for(i=0;i<5;i++) { ioservice.post(boost::bind(dowork,10)); } std::cout<<"size="<<threadpool.size()<<std::endl; work.reset(); ioservice.reset(); ioservice.run(); } }
please give me suggestion.i got stuck with it.
You are indeed correctly starting 100 threads each run()ing the io_service. However, io_service.reset() in each of the iterations in the while loop completely destroys that. You're then run()ing in the main thread, which blocks until all pending operations are completed. You shall only run() on the threads where you want to block and dispatch handlers. You shall only reset() when you really mean it. Strongly reconsider your logic in the main thread and find out what the functions you call actually do. Also, your posting contains A LOT of whitespace and even repeats the message several times. Look into if your mail client is broken. -- Lars Viklund | zao@acc.umu.se _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
On Sun, Jul 13, 2014 at 08:11:23PM +0800, imran sheikh wrote:
hi ,
but if i am not using ioservice.reset() in the while loop then second time the threads are not executing properly. so please give me a strong running solution .i need immediately this one.
Hi there, You appear to be missing some knowledge about how the operations on an io_service fits together. When you post a handler to an io_service, it will (eventually) run on any thread that happens to do any of run(), run_one(), poll(), poll_one(). The io_service will be in the running state as long as there exists 'work', operations in progress (like send/recv), or posted handlers in queue. Important here is that if the io_service runs out of things to do, it finishes and returns from the run() functions. The typical lifecycle of an io_service are: == Setup == * prepare and post operations to it; * construct instances of 'work' to artificially keep it alive; * run one or more run()/run_one()/poll()/poll_one() somewhere. == Runtime == * do things with devices, timers, post handlers, etc. == Teardown == * destroy any artifical 'work' objects that you use to keep it running; * stop posting more operations and handlers to it, so it runs out; * optionally wait for it and any worker threads to finish. For the scenario of a thread pool that runs posted handlers; the typical approach is: * make control 'work'; * create N threads, running run(); * post handlers until bored; * destroy control 'work'; * join all N threads. The reset() function shall only be called after the io_service is stopped and you wish to eventually re-start it [1]. It's an error to call reset() while you're run()ing. [1] http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/reference/io_servic... -- Lars Viklund | zao@acc.umu.se
hi lars, i done like this. ->create ioservice. ->created work object. ->creating N threads. ->running ioservice. -> now in while loop, -> in for loop posting 5 times. ->work.reset() ->joinall ->continue while loop. now, problem are, ->posting ioservice 5 times. but in that some threads are same only. so why all are not diff? ->and second time its not executing any threads why? give me one example like this. and suggest please. thanks. thanks, imran On Sunday, 13 July 2014 6:22 PM, Lars Viklund <zao@acc.umu.se> wrote: On Sun, Jul 13, 2014 at 08:11:23PM +0800, imran sheikh wrote:
hi ,
but if i am not using ioservice.reset() in the while loop then second time the threads are not executing properly. so please give me a strong running solution .i need immediately this one.
Hi there, You appear to be missing some knowledge about how the operations on an io_service fits together. When you post a handler to an io_service, it will (eventually) run on any thread that happens to do any of run(), run_one(), poll(), poll_one(). The io_service will be in the running state as long as there exists 'work', operations in progress (like send/recv), or posted handlers in queue. Important here is that if the io_service runs out of things to do, it finishes and returns from the run() functions. The typical lifecycle of an io_service are: == Setup == * prepare and post operations to it; * construct instances of 'work' to artificially keep it alive; * run one or more run()/run_one()/poll()/poll_one() somewhere. == Runtime == * do things with devices, timers, post handlers, etc. == Teardown == * destroy any artifical 'work' objects that you use to keep it running; * stop posting more operations and handlers to it, so it runs out; * optionally wait for it and any worker threads to finish. For the scenario of a thread pool that runs posted handlers; the typical approach is: * make control 'work'; * create N threads, running run(); * post handlers until bored; * destroy control 'work'; * join all N threads. The reset() function shall only be called after the io_service is stopped and you wish to eventually re-start it [1]. It's an error to call reset() while you're run()ing. [1] http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/reference/io_servic... -- Lars Viklund | zao@acc.umu.se _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Hi, I tried to follow your suggestion by not using ioservice.reset By doing so, it is not entering into the while loop again for xexcuting another query with the set of threads in the pool. I was using work.reset to merge the results that come out of my first iteration of while loop After every iteration, I need to merge results that are computed by the threads. So, I used work.reset and again using post in the loop to post qnother query for the set of threads in the pool. Please review my piece of code. Iam sure that I missed some valuable point in my code that is causing this strange behaviour. Please suggest me a solution for this as I got stuck with the same. Thanks for your immediate responses. Regards, On Sunday, 13 July 2014 6:22 PM, Lars Viklund <zao@acc.umu.se> wrote: On Sun, Jul 13, 2014 at 08:11:23PM +0800, imran sheikh wrote:
hi ,
but if i am not using ioservice.reset() in the while loop then second time the threads are not executing properly. so please give me a strong running solution .i need immediately this one.
Hi there, You appear to be missing some knowledge about how the operations on an io_service fits together. When you post a handler to an io_service, it will (eventually) run on any thread that happens to do any of run(), run_one(), poll(), poll_one(). The io_service will be in the running state as long as there exists 'work', operations in progress (like send/recv), or posted handlers in queue. Important here is that if the io_service runs out of things to do, it finishes and returns from the run() functions. The typical lifecycle of an io_service are: == Setup == * prepare and post operations to it; * construct instances of 'work' to artificially keep it alive; * run one or more run()/run_one()/poll()/poll_one() somewhere. == Runtime == * do things with devices, timers, post handlers, etc. == Teardown == * destroy any artifical 'work' objects that you use to keep it running; * stop posting more operations and handlers to it, so it runs out; * optionally wait for it and any worker threads to finish. For the scenario of a thread pool that runs posted handlers; the typical approach is: * make control 'work'; * create N threads, running run(); * post handlers until bored; * destroy control 'work'; * join all N threads. The reset() function shall only be called after the io_service is stopped and you wish to eventually re-start it [1]. It's an error to call reset() while you're run()ing. [1] http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/reference/io_servic... -- Lars Viklund | zao@acc.umu.se _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
imran sheikh
-
Lars Viklund