Boost logo

Boost Users :

Subject: Re: [Boost-users] Fw: boost::asio
From: Lars Viklund (zao_at_[hidden])
Date: 2014-07-12 19:50:18


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_at_[hidden]

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net