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.
boost asio scalability and multithreading的更多相关文章
- boost::asio译文
Christopher Kohlhoff Copyright © 2003-2012 Christopher M. Kohlhoff 以Boost1.0的软件授权进行发布(见附带的LICENS ...
- Boost.Asio技术文档
Christopher Kohlhoff Copyright © 2003-2012 Christopher M. Kohlhoff 以Boost1.0的软件授权进行发布(见附带的LICENSE_1_ ...
- c++ boost asio库初学习
前些日子研究了一个c++的一个socket库,留下范例代码给以后自己参考. 同步server: // asio_server.cpp : コンソール アプリケーションのエントリ ポイントを定義します. ...
- 如何在多线程leader-follower模式下正确的使用boost::asio。
#include <assert.h> #include <signal.h> #include <unistd.h> #include <iostream& ...
- BOOST.Asio——Tutorial
=================================版权声明================================= 版权声明:原创文章 谢绝转载 啥说的,鄙视那些无视版权随 ...
- BOOST.Asio——Overview
=================================版权声明================================= 版权声明:原创文章 谢绝转载 啥说的,鄙视那些无视版权随 ...
- boost asio sync
Service: #include<boost/asio.hpp> #include<boost/thread.hpp> #include<iostream> #i ...
- 网络库crash以及boost asio strand dispath分析
最近在做服务器的稳定性的相关测试,服务器的网络底层使用的是boost asio,然后自己做的二次封装以更好的满足需求. 服务器昨天晚上发现crash了一次,之前测试了将近半个多月,有一次是莫名的退出了 ...
- boost asio tcp server 拆分
从官方给出的示例中对于 boost::asio::ip::tcp::acceptor 类的使用,是直接使用构造函数进行构造对象,这一种方法用来学习是一个不错的方式. 但是要用它来做项目却是不能够满足我 ...
随机推荐
- EZOJ #393加倍的飞机
分析 从大到小考虑每个点 记录一个连通块中选了选了几个 如果选的小于siz则直接选否则不选 代码 #include<bits/stdc++.h> using namespace std; ...
- spring-cloud eureka注册发现
idea新建一个eureka server服务 application.yml 配置: spring: application: name: eureka-server server: port: 7 ...
- koa2 源码解读 application
koa2的源码比较简单,重点解读aplication, 其中context源码比较简单,主要是一些error cookies等,重点可以关注下delegate,delegate模块中,主要通过prot ...
- 使用Xmanager远程CentOS 7服务器(XDMCP)
0. 前言 基本概念 简略概述 Display Manager 提供登录需求 在文字界面下可以通过startx来启动Xwindows 在runlevel 5下,在tty7处有可以使用的图形登录界面(方 ...
- Orcle获取当前时间加小时
如下是oracle 获取当前数据库时间加2个小时 select to_date(TO_CHAR (SYSDATE, 'yyyy-mm-dd hh24:mi:ss'),'yyyy-mm-dd hh24: ...
- ELK+Filebeat (2)
ELK+Filebeat收集多台机器不同日志 采坑:在使用了6.0版本的ELK以后,使用如上配置,if [type]匹配不到在filebeat里面使用document_type定义的字符串.在多次调试 ...
- Python 学习笔记16 类 - 导入
我们在编码的过程中,可能会给对象添加越来越多的功能,即使我们使用了继承,也不可避免的使文件越来越臃肿. 为了避免这种情况, Python允许将对象存储在模块中,并且可以在其他模块中进行导入. 其实这和 ...
- phpcms列表分页ajax加载更多
1.在phpcms\modules\content\index.php文件中添加以下函数: /*列表分页ajax加载更多*/ public function homeajaxlist() { if( ...
- hive拉链表以及退链例子笔记
拉链表设计: 在企业中,由于有些流水表每日有几千万条记录,数据仓库保存5年数据的话很容易不堪重负,因此可以使用拉链表的算法来节省存储空间. 例子: -- 用户信息表; 采集当日全量数据存储到 (当日 ...
- vmware linux root密码破解
centOS: 1.开机过程按上下箭头键,让系统不要进入到引导程序中, 2.按 'e' 进入到编辑模式 3.找到linux16开始的首行,在末尾加入'rw init=/bin/sh' (会出现修改密码 ...