Boost logo

Boost Users :

Subject: Re: [Boost-users] asio - improving chat server example
From: Lars Viklund (zao_at_[hidden])
Date: 2012-05-22 10:46:09


On Tue, May 22, 2012 at 05:38:27PM +0400, Marat Abrarov wrote:
> Hi, John.
>
> > As you recall, I am using boost/Threads to execute io_service::run() to allow my server to have a
> > main-thread application loop. The use of threads have complicated my application design by giving me
> > problems to join the thread_group upon shutdown.
> >
> > I just had the thought, maybe I can tickle the io_service each frame of my main-thread loop instead of
> > using Threads? Is that the motivation for io_service::poll() ? Maybe using io_service.poll() will
> > allow me to avoid the need for threads, at least while I'm proving the concept. What do you think?
>
> I don't think asio::io_service::poll() can help you. The only solutions I can suggest I've described before. It isn't
> related to Asio - it is related to proactor pattern.

If you have a soft-realtime main loop as is common in games, it's
perfectly fine to invoke poll() instead of having separate threads
running run().

My canonical main loop tends to be:
  while (1)
  {
    PeekOSMessages();
    main_io.poll();
    simulate();
    render();
  }

This has the added benefit of executing in the main thread, so you don't
need as much synchronization and checking for changes compared to
running handlers in different threads.

Personally, I have
* an io_service for post()ing callbacks to the main thread,
* an io_service with a bunch of worker threads for small network jobs,
* an io_service with low-priority threads for bulk deferred work.

This tends to cover all the workloads I encounter in a networked game
client.

-- 
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