Boost::asio 并发性(?)

发布于 2024-11-19 05:14:23 字数 503 浏览 2 评论 0原文

我正在编译 boost::asio 示例: http://www.boost .org/doc/libs/1_46_1/doc/html/boost_asio/examples.html#boost_asio.examples.http_server_3

应该是一个多线程服务器实现。

现在,在 async_read 处理程序中,我打印一条消息并休眠 30 秒。我在两个浏览器中打开 localhost,看到handle_read被调用一次,然后30秒没有任何反应,最后handle_read被第二次调用。

使用 5 个线程调用 io_Service.run。

为什么处理程序没有同时调用?例如,为什么它要等待第一个handle_read完成后再调用第二个?

I am compiling the boost::asio example:
http://www.boost.org/doc/libs/1_46_1/doc/html/boost_asio/examples.html#boost_asio.examples.http_server_3

It is supposed to be a multi-threaded server implementation.

Now, in the async_read handler I print a message and sleep for 30 seconds. I open localhost in two browsers and see that handle_read is called once, then 30 seconds nothing happens and finally handle_read is called for the second time.

The io_Service.run is called with 5 threads.

Why aren't the handlers called concurrently? e.g. why does it wait for the first handle_read to finish before calling the second?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遮云壑 2024-11-26 05:14:23

它有助于发布代码。当我对asio示例使用此修改时:

void connection::handle_read(const boost::system::error_code& e,
    std::size_t bytes_transferred)
{
  std::cerr << "connection::handle_read()\n";
  boost::this_thread::sleep(boost::posix_time::seconds(10));
  std::cerr << "connection::handle_read() done sleep\n";
  if (!e)
  {

它按预期工作,即

$ ./test 0.0.0.0 7777 5 .
connection::handle_read()
connection::handle_read()
connection::handle_read() done sleep
connection::handle_read() done sleep

How do you“sleep for 30秒”?也许您使用的 sleep 函数会暂停进程中的所有线程?

It helps to post code. When I use this modification to the asio example:

void connection::handle_read(const boost::system::error_code& e,
    std::size_t bytes_transferred)
{
  std::cerr << "connection::handle_read()\n";
  boost::this_thread::sleep(boost::posix_time::seconds(10));
  std::cerr << "connection::handle_read() done sleep\n";
  if (!e)
  {

it works as expected, that is

$ ./test 0.0.0.0 7777 5 .
connection::handle_read()
connection::handle_read()
connection::handle_read() done sleep
connection::handle_read() done sleep

How do you "sleep for 30 seconds"? Perhaps the sleep function you've used pauses all threads in the process?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文