Boost logo

Boost :

From: Chris Richards (chris.richards_at_[hidden])
Date: 2006-11-08 11:54:24


Even though the exception is caught it causes both the socket thread and the acceptor thread to exit.

For example, if I have 2 threads running, with both boost::asio::ip::tcp::acceptor::async_accept and boost::asio::ip::tcp::socket::async_read_some operations outstanding, then if I call StopThread in the code shown below I would expect only one thread to exit.

  void IoServiceRunner::ThreadFn()
  {
    try {
      ioService_.run();
    }
    catch (StopThreadException& ex) {
      std::cout << "StopThreadException occurred" << std::endl;
    }
    catch (std::exception& ex) {
      std::cout << "Exception occurred" << ex.what() << std::endl;
    }
    catch (...) {
      std::cout << "Unknown exception occurred");
    }
  }

  void IoServiceRunner::StopThread()
  {
    ioService_.post(boost::bind(&IoServiceRunner::NullThreadFn, this));
  }

  void IoServiceRunner::NullThreadFn()
  {
    throw StopThreadException();
  }

From: Felipe Magno de Almeida
Sent: Wed 08/11/2006 16:13
To: boost_at_[hidden]
Subject: Re: [boost] asio & variable number of worker threads

Just catch the exception and exit the thread gracefully.

On 11/8/06, Chris Richards <chris.richards_at_[hidden]> wrote:
> I have implemented a simple tcp server using asio. I wish to be able to
> dynamically add and remove threads from the io_service thread pool, so that
> when the server is heavily loaded it can be given more threads to cope with
> the load, but when the load reduces it can release the threads back to the OS.
>
> I've tried posting to the io_service a handler which just throws an exception
> so that the thread exits, but it also resets the tcp connection and causes the
> acceptor thread to exit as well.
>
> Is there a way of cleanly stopping just a single io_service::run method which
> would then cause the thread to exit?
> _______________________________________________
> Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
>

-- 
Felipe Magno de Almeida
_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk