开发者

boost:asio HTTP server example 3 thread count (Windows build)

In example 3 of the HTTP server (boost 2.44), the IO service is created without a thread count hint. Under Windows, one normally passes the thread count to CreateIoCompletionPort(). boost:asio has an IO Service ctor that takes the thread count, but that ctor is not used in this example. And the thread count is known.

My question is: is there a reason to create the IO Service without the thread count? Does boost:asio assume one would never create more threads than one per core? Note if the thread count passed to Creat开发者_Python百科eIoCompletionPort() is zero, the system will allow one thread per core concurrently running threads.


When you call the parameter-less constructor on io_service, the call to CreateIoCompletionPort winds up using a thread count of 0xffffffff in the code here:

void win_iocp_io_service::init(size_t concurrency_hint)
{
  iocp_.handle = ::CreateIoCompletionPort(INVALID_HANDLE_VALUE, 0, 0,
      static_cast<DWORD>((std::min<size_t>)(concurrency_hint, DWORD(~0))));
  if (!iocp_.handle)
  {
    DWORD last_error = ::GetLastError();
    boost::system::error_code ec(last_error,
        boost::asio::error::get_system_category());
    boost::asio::detail::throw_error(ec, "iocp");
  }
}

Not sure how Windows interprets this but the call works OK, so I assume this is the same as using 0. I guess the assumption is that the OS knows best?


When concurrency_hint is not specified to boost:asio::io_service() constructor. Default is infinite or unbounded, which is never the best practice. But the designer probably felt this Windows based concurrency_hint was unnecessary, so made the default consistent with all platforms (no limit). This is likely to be consistent with how other OS interpret this value.

Since Windows itself creates no threads out of this whole fiasco, it shouldn't even care. concurrency_hint should be renamed max_allowed_concurrency_on_Windows

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜