boost::asio学习 - io_service的run,run_one,poll,poll_one的区别

本文深入解析了Windows I/O服务的工作流程,包括核心函数do_one的执行机制,以及run、run_one、poll和poll_one等函数的区别与实现方式。

在这几个函数里其实都是调用了do_one这个函数,而这个函数的作用就是从获取完成端口状态,所有定时器以及网络事件都是通过do_one来调度的,do_one的函数原型:

size_t do_one(bool block, boost::system::error_code& ec)


它的第一个参数说明了是否阻塞,在do_one代码中:

BOOL ok = ::GetQueuedCompletionStatus(iocp_.handle, &bytes_transferred, &completion_key, &overlapped, block ? timeout : 0);




可以看到如果为false,将等待时间为0(即不阻塞)。

 

而run,run_one,及poll,poll_one的实现也相当简单,如下:

// Run the event loop until stopped or no more work.
size_t run(boost::system::error_code& ec)
{
  if (::InterlockedExchangeAdd(&outstanding_work_, 0) == 0)
  {
    stop();
    ec = boost::system::error_code();
    return 0;
  }

  call_stack<win_iocp_io_service>::context ctx(this);

  size_t n = 0;
  while (do_one(true, ec))
    if (n != (std::numeric_limits<size_t>::max)())
      ++n;
  return n;
}

// Run until stopped or one operation is performed.
size_t run_one(boost::system::error_code& ec)
{
  if (::InterlockedExchangeAdd(&outstanding_work_, 0) == 0)
  {
    stop();
    ec = boost::system::error_code();
    return 0;
  }

  call_stack<win_iocp_io_service>::context ctx(this);

  return do_one(true, ec);
}

// Poll for operations without blocking.
size_t poll(boost::system::error_code& ec)
{
  if (::InterlockedExchangeAdd(&outstanding_work_, 0) == 0)
  {
    stop();
    ec = boost::system::error_code();
    return 0;
  }

  call_stack<win_iocp_io_service>::context ctx(this);

  size_t n = 0;
  while (do_one(false, ec))
    if (n != (std::numeric_limits<size_t>::max)())
      ++n;
  return n;
}

// Poll for one operation without blocking.
size_t poll_one(boost::system::error_code& ec)
{
  if (::InterlockedExchangeAdd(&outstanding_work_, 0) == 0)
  {
    stop();
    ec = boost::system::error_code();
    return 0;
  }

  call_stack<win_iocp_io_service>::context ctx(this);

  return do_one(false, ec);
}


可以看到run其实就是一直循环执行do_one,并且是以阻塞方式进行的(参数为true),而run_one同样是以阻塞方式进行的,但只执行一次do_one;poll和run几乎完全相同,只是它是以非阻塞方式执行do_one(参数为false),poll_one是以非阻塞方式执行一次do_one。

 

原文链接:http://my.oschina.net/jackwgm/blog/6314

[root@dcvserver03 Sunshine]# mkdir build mkdir: cannot create directory ‘build’: File exists [root@dcvserver03 Sunshine]# cd build [root@dcvserver03 build]# cmake .. \ > -DCMAKE_BUILD_TYPE=Release \ > -DBOOST_ROOT=/usr/local \ > -DBOOST_LIBRARYDIR=/usr/local/lib \ > -DMINIUPNP_INCLUDE_DIR=/usr/local/include \ > -DMINIUPNP_LIBRARY=/usr/local/lib/libminiupnpc.a /root/py/Sunshine Sunshine Branch: master PROJECT_NAME: Sunshine PROJECT_VERSION: 0.0.0 PROJECT_VERSION_MAJOR: 0 PROJECT_VERSION_MINOR: 0 PROJECT_VERSION_PATCH: 0 CMAKE_PROJECT_VERSION: 0.0.0 CMAKE_PROJECT_VERSION_MAJOR: 0 CMAKE_PROJECT_VERSION_MINOR: 0 CMAKE_PROJECT_VERSION_PATCH: 0 PROJECT_YEAR: 1990 PROJECT_MONTH: 01 PROJECT_DAY: 01 CMake Warning (dev) at cmake/macros/common.cmake:27 (_find_package): Policy CMP0144 is not set: find_package uses upper-case <PACKAGENAME>_ROOT variables. Run "cmake --help-policy CMP0144" for policy details. Use the cmake_policy command to set the policy and suppress this warning. CMake variable BOOST_ROOT is set to: /usr/local For compatibility, find_package is ignoring the variable, but code in a .cmake module might still use it. Call Stack (most recent call first): cmake/dependencies/Boost_Sunshine.cmake:33 (find_package) cmake/dependencies/common.cmake:5 (include) CMakeLists.txt:61 (include) This warning is for project developers. Use -Wno-dev to suppress it. -- Could NOT find Boost (missing: Boost_DIR) -- Boost v1.89.0 package not found in the system. Falling back to FetchContent. -- Boost: Release build, static libraries, MPI OFF, Python OFF, testing OFF -- Boost: libraries included: filesystem;locale;log;program_options;system;asio;crc;format;process;property_tree -- Boost.Charconv: quadmath support ON -- Boost.Context: architecture x86_64, binary format elf, ABI sysv, assembler gas, suffix .S, implementation fcontext -- Found the following ICU libraries: -- data (required): /usr/lib64/libicudata.so -- i18n (required): /usr/lib64/libicui18n.so -- uc (required): /usr/lib64/libicuuc.so -- Found ICU: /usr/include (found suitable version "50.2", minimum required is "4.8.1") -- Boost.Locale: iconv ON, ICU ON, POSIX ON, std ON, winapi OFF -- Found the following ICU libraries: -- data (required): /usr/lib64/libicudata.so -- i18n (required): /usr/lib64/libicui18n.so -- uc (required): /usr/lib64/libicuuc.so -- Found ICU: /usr/include (found version "50.2") -- Boost.Thread: threading API is pthread -- Boost include dirs: $<BUILD_INTERFACE:/root/py/Sunshine/build/_deps/boost-src/libs/headers/include> -- Boost libraries: Boost::filesystem;Boost::locale;Boost::log;Boost::program_options;Boost::system;Boost::asio;Boost::crc;Boost::format;Boost::process;Boost::property_tree -- Looking for fcntl -- Looking for fcntl - found -- Looking for ioctl -- Looking for ioctl - found -- Looking for poll -- Looking for poll - found -- Looking for getaddrinfo -- Looking for getaddrinfo - found -- Looking for getnameinfo -- Looking for getnameinfo - found -- Looking for gethostbyname_r -- Looking for gethostbyname_r - found -- Looking for gethostbyaddr_r -- Looking for gethostbyaddr_r - found -- Looking for inet_pton -- Looking for inet_pton - found -- Looking for inet_ntop -- Looking for inet_ntop - found -- Performing Test HAS_MSGHDR_FLAGS -- Performing Test HAS_MSGHDR_FLAGS - Success -- Check size of socklen_t -- Check size of socklen_t - done -- Check size of QOS_FLOWID -- Check size of QOS_FLOWID - failed -- Check size of PQOS_FLOWID -- Check size of PQOS_FLOWID - failed -- nlohmann_json v3.11.x package not found in the system. Falling back to FetchContent. -- Using the multi-header code from /root/py/Sunshine/build/_deps/json-src/include/ CMake Warning at third-party/libdisplaydevice/src/CMakeLists.txt:14 (message): Linux is not supported yet. -- nlohmann_json v3.11.x package not found in the system. Falling back to FetchContent. -- Checking for module 'miniupnpc' -- No package 'miniupnpc' found CMake Error at /usr/local/share/cmake-3.28/Modules/FindPkgConfig.cmake:619 (message): The following required packages were not found: - miniupnpc Call Stack (most recent call first): /usr/local/share/cmake-3.28/Modules/FindPkgConfig.cmake:841 (_pkg_check_modules_internal) cmake/dependencies/common.cmake:26 (pkg_check_modules) CMakeLists.txt:61 (include) -- Configuring incomplete, errors occurred! [root@dcvserver03 build]# [root@dcvserver03 build]# make -j$(nproc) VERBOSE=1 # 显示详细编译日志 make: *** No targets specified and no makefile found. Stop. [root@dcvserver03 build]# sudo make install make: *** No rule to make target `install'. Stop. [root@dcvserver03 build]# [root@dcvserver03 build]#
最新发布
11-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值