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