A library such as Boost.Asio is typically used to achieve greater efficiency. With no need to wait for an operation to finish, a program can perform other tasks in between. Therefore, it is possible to start several asynchronous operations that are all executed concurrently - remember that asynchronous operations are usually used to access resources outside of a process. Since these resources can be different devices, they can work independently and execute operations concurrently.

#include <boost/asio/io_service.hpp>
#include <boost/asio/steady_timer.hpp>
#include <chrono>
#include <thread>
#include <iostream> using namespace boost::asio; int main() {
io_service ioservice; steady_timer timer1{ioservice, std::chrono::seconds{}};
timer1.async_wait([](const boost::system::error_code& ec)
{ std::cout << "3 sec\n"; }); steady_timer timer2{ioservice, std::chrono::seconds{}};
timer2.async_wait([](const boost::system::error_code& ec)
{ std::cout << "3 sec\n"; }); std::thread thread1{[&ioservice](){ ioservice.run(); }};
std::thread thread2{[&ioservice](){ ioservice.run(); }}; thread1.join();
thread2.join(); return ;
}

both alarm clocks should ring after three seconds. Because two threads are available, both lambda functions can be executed concurrently. If the second alarm clock rings while the handler of the first alarm stock is being executed, the hanler can be executed in the second thread. If the handler of the first alarm clock has already returned, the I/O service object can use any thread to executed the second handler.

最新文章

  1. Python学习06——列表的操作(2)
  2. MongoDB的安装及CURD操作
  3. 【转】group by多个字段理解
  4. Counting sheep...
  5. (C语言)char类型与int类型相加
  6. vs2015开发Windows服务
  7. 以Random Forests和AdaBoost为例介绍下bagging和boosting方法
  8. 关于DB2版本、补丁升级和回退的总结[转载]
  9. java设计模式-State(状态)模式
  10. [USACO17JAN]Subsequence Reversal序列反转
  11. CUDA学习,第一个kernel函数及代码讲解
  12. java核心技术-(总结自杨晓峰-java核心技术36讲)
  13. [20181108]with temp as 建立临时表吗.txt
  14. python2.7安装和uwsgi
  15. javascript实现限定高度下文字随不同设备自适应改变字体大小至字数完全展示
  16. Eclipse:构造函数不提示才发现
  17. HTML第五章总结
  18. Python判断变量的数据类型的两种方法
  19. 2018.07.04 BZOJ 2823: AHOI2012信号塔(最小圆覆盖)
  20. 打包python脚本为exe的坎坷经历, by pyinstaller方法

热门文章

  1. codecs模块, decode、encode
  2. HDU6424 Rikka with Time Complexity
  3. 126B Password[扩展kmp学习]
  4. P2239螺旋矩阵
  5. MySQL 1130错误,无法远程连接
  6. debian7下安装eclipse
  7. jQuery DataTables 分页
  8. Map2
  9. mybatis 批量update报语法错误解决方法
  10. Java数组模拟栈