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的更多相关文章

  1. boost::asio译文

        Christopher Kohlhoff Copyright © 2003-2012 Christopher M. Kohlhoff 以Boost1.0的软件授权进行发布(见附带的LICENS ...

  2. Boost.Asio技术文档

    Christopher Kohlhoff Copyright © 2003-2012 Christopher M. Kohlhoff 以Boost1.0的软件授权进行发布(见附带的LICENSE_1_ ...

  3. c++ boost asio库初学习

    前些日子研究了一个c++的一个socket库,留下范例代码给以后自己参考. 同步server: // asio_server.cpp : コンソール アプリケーションのエントリ ポイントを定義します. ...

  4. 如何在多线程leader-follower模式下正确的使用boost::asio。

    #include <assert.h> #include <signal.h> #include <unistd.h> #include <iostream& ...

  5. BOOST.Asio——Tutorial

    =================================版权声明================================= 版权声明:原创文章 谢绝转载  啥说的,鄙视那些无视版权随 ...

  6. BOOST.Asio——Overview

    =================================版权声明================================= 版权声明:原创文章 谢绝转载  啥说的,鄙视那些无视版权随 ...

  7. boost asio sync

    Service: #include<boost/asio.hpp> #include<boost/thread.hpp> #include<iostream> #i ...

  8. 网络库crash以及boost asio strand dispath分析

    最近在做服务器的稳定性的相关测试,服务器的网络底层使用的是boost asio,然后自己做的二次封装以更好的满足需求. 服务器昨天晚上发现crash了一次,之前测试了将近半个多月,有一次是莫名的退出了 ...

  9. boost asio tcp server 拆分

    从官方给出的示例中对于 boost::asio::ip::tcp::acceptor 类的使用,是直接使用构造函数进行构造对象,这一种方法用来学习是一个不错的方式. 但是要用它来做项目却是不能够满足我 ...

随机推荐

  1. PHP-利用二叉堆实现TopK-算法

    介绍 在以往工作或者面试的时候常会碰到一个问题,如何实现海量TopN,就是在一个非常大的结果集里面快速找到最大的前10或前100个数,同时要保证内存和速度的效率,我们可能第一个想法就是利用排序,然后截 ...

  2. rssi pdf 单双峰正态发布 与 定位

  3. pygame应用——生产者消费者模型

    因为操作系统的一个生产者-消费者拓展作业,以一个飞机大战的模型修改来的 import pygame import time from pygame.locals import * bulletsNum ...

  4. jmeter动态修改线程组参数

    jmeter非gui模式修改线程属性进行性能测试 在使用JMeter进行性能测试自动化时,如果按照平常的非Gui模式就是脚本每次修改参数都需要在gui模式下修改保存之后,然后在非gui模式之后运行,这 ...

  5. C#调取接口时报错:服务器提交了协议冲突. Section=ResponseStatusLine

    private Dictionary<string, Object> GetLocation(string imei) { #region===代码=== string serviceAd ...

  6. HTML DOM cursor 属性

    值 描述 url 需被使用的自定义光标的URL 注释:请在此列表的末端始终定义一种普通的光标,以防没有由 URL 定义的可用光标. default 默认光标(通常是一个箭头) auto 默认.浏览器设 ...

  7. mysql时间增加一年

    update siteserver_content_57 set AddDate=DATE_ADD(AddDate,INTERVAL 2 year),LastHitsDate=DATE_ADD(Las ...

  8. Java_1.Java符号体系

    Java符号包含五类:标识符.关键字.常量及字面量.运算符.分隔符 1.标识符 定义:用于标明程序中元素的名字,如类.方法和变量 命名规则: ·由字母.数字.下划线(_)和美元符号($)构成的字母序列 ...

  9. dp(最长公共上升子序列)

    This is a problem from ZOJ 2432.To make it easyer,you just need output the length of the subsequence ...

  10. [Codeforces 865C]Gotta Go Fast(期望dp+二分答案)

    [Codeforces 865C]Gotta Go Fast(期望dp+二分答案) 题面 一个游戏一共有n个关卡,对于第i关,用a[i]时间通过的概率为p[i],用b[i]通过的时间为1-p[i],每 ...