zoukankan      html  css  js  c++  java
  • 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{3}};
      timer1.async_wait([](const boost::system::error_code& ec)
         {  std::cout << "3 sec
    "; });
    
      steady_timer timer2{ioservice, std::chrono::seconds{3}};
      timer2.async_wait([](const boost::system::error_code& ec)
        {  std::cout << "3 sec
    ";  });
    
      std::thread thread1{[&ioservice](){ ioservice.run(); }};
      std::thread thread2{[&ioservice](){  ioservice.run(); }};
    
      thread1.join();
      thread2.join();
    
      return 0;
    }

    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.

  • 相关阅读:
    VB.NET与C# 语法区别展示
    利用 ASP.NET 的内置功能抵御 Web 攻击 (1)
    .NET 中获取调用方法名
    C# 6.0 的那些事
    .NET基础之自定义泛型
    汽车学习---汽车知识大全【all】
    Django学习---抽屉热搜榜分析【all】
    Python 系统学习梳理_【All】
    Python学习---装饰器/迭代器/生成器的学习【all】
    Java 系统学习梳理_【All】
  • 原文地址:https://www.cnblogs.com/sssblog/p/11264540.html
Copyright © 2011-2022 走看看