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 类的使用,是直接使用构造函数进行构造对象,这一种方法用来学习是一个不错的方式. 但是要用它来做项目却是不能够满足我 ...
随机推荐
- toutiao url
https://it.snssdk.com/article/v2/tab_comments/?group_id=6485899113563947533&item_id=648589911356 ...
- 将python文件打包成exe可执行文件
操作系统:win8-64位 python版本:3.5 pyInstaller版本:3.2(下载地址:http://www.pyinstaller.org/) pywin32版本:pywin32-219 ...
- 永远让比较函数对相等的值返回false
今天在刷OJ的时候,有一道题一直Runtime Error,查错出来是比较函数写挂掉了,但是不知道错误在哪,于是查阅资料:永远让比较函数对相等的值返回false 具体可点击此处查看分析:链接 另外,在 ...
- 图解Http阅读笔记(一)
1.网络基础 TCP/IP 1.1TCP /IP 协议族 计算机与网络设备要相互通信,双方就必须基于相同的方法.比如,如何探测到通信目标.由哪一边先发起通信.使用哪种语言进行通信.怎样结束通信等规 ...
- react 渲染顺序
工作中要对一个表格做再次更新, 可能是渲染后更新或者部分组件渲染之后, 对页面效果做处理 之前对react的理解, 仅仅停留在render渲染. 这次好好理解了下react的生命周期 1 react组 ...
- Linux-第二篇常用命令
1.常用目录文件操作命令 cd:切换目录 格式:cd 目录 ls:显示文件和目录列表.可选参数: -l 列出文件的详细信息 -a 列出当前目录所有文件,包含隐藏文件 ll:查看目录接口,相当于是ls ...
- 批量调整word 图片大小
打开文档后,按Alt+F11,在左边Porject下找到ThisDocument,右键插入模块,贴上下面的 Sub Macro()For Each iShape In ActiveDocument.I ...
- 这样设计 Java 异常更优雅,赶紧学!
来源:lrwinx.github.io/2016/04/28/如何优雅的设计java异常/ 导语 异常处理是程序开发中必不可少操作之一,但如何正确优雅的对异常进行处理确是一门学问,笔者根据自己的开发经 ...
- #3831 TJOI2013单词
WOJ#3831 TJOI2013单词 题面 某人读论文,一篇论文是由许多单词组成.但他发现一个单词会在论文中出现很多次,现在想知道每个单词分别在论文中出现多少次. 输入 第一个一个整数 N ,表示有 ...
- python------生产者消费者模型 和 管道
一.为什么要使用生产者和消费者? 在线程世界里,生产者就是生产数据的线程,消费者就是消费数据的线程,在多线程开发当中,如果生产者处理速度很快,而消费者处理速度很慢,那么生产者就必须等待消费者处理完,才 ...