Boost.Asio——(6)多线程中的同步回调

1 源码及注释

//多线程同步
//单线程程序开发不适用于服务器,当出现以下两种限制时,最好使用多线程开发:
//(1)有些程序操作需要耗费很多时间
//(2)计算机具有多核多处理器,程序需要充分使用计算机硬件资源

//当使用多线程时,很可能出现多个程序调用同一个资源,但是这个资源线程不安全,此时我们需要一种方法使现成同步
#include "stdafx.h"
#include <iostream>
#include <boost/asio.hpp>
#include <boost/thread/thread.hpp>      //用于多线程控制
#include <boost/bind.hpp>               //用于参数传递
#include <boost/date_time/posix_time/posix_time.hpp>//定时器相关头文件

//建立printer类,这个类中将有两个并行执行的timer
class printer
{
public:
    //这里初始化了两个timer和一个strand,strand可以保证通过它分配的任务,一个执行完成之后另一个才会开始执行。
    printer(boost::asio::io_service& io)
        : strand_(io),
        timer1_(io, boost::posix_time::seconds(1)),
        timer2_(io, boost::posix_time::seconds(1)),
        count_(0)
    {
        //在这里我们使用同一个strand打包了timer,strand会保证两个timer不同时执行。反过来说,如果我们使用不同的strand打包,他们是可以同时执行的。
        timer1_.async_wait(strand_.wrap(boost::bind(&printer::print1, this)));
        timer2_.async_wait(strand_.wrap(boost::bind(&printer::print2, this)));
    }

    ~printer()
    {
        std::cout << "Final count is " << count_ << std::endl;
    }
    //要明白的是,多线程编程中,如果不同线程需要共享相同的资源,这个时候就需要进行线程同步(这是汉语翻译的不足之处,所谓的同步应该说是资源的协调,使用共同的资源需要排队进行)
    //在这段程序中,共享的资源是std::cout 和 the count_
    void print1()
    {
        if (count_ < 10)
        {
            std::cout << "Timer 1: " << count_<<"  thread id:" <<GetCurrentThreadId() << std::endl;
            ++count_;

            timer1_.expires_at(timer1_.expires_at() + boost::posix_time::seconds(1));
            timer1_.async_wait(strand_.wrap(boost::bind(&printer::print1, this)));
        }
    }

    void print2()
    {
        if (count_ < 10)
        {
            std::cout << "Timer 2: " << count_ << "  thread id:" << GetCurrentThreadId() << std::endl;
            ++count_;

            timer2_.expires_at(timer2_.expires_at() + boost::posix_time::seconds(1));
            timer2_.async_wait(strand_.wrap(boost::bind(&printer::print2, this)));
        }
    }

private:
    boost::asio::io_service::strand strand_;
    boost::asio::deadline_timer timer1_;
    boost::asio::deadline_timer timer2_;
    int count_;
};

int main()
{
    boost::asio::io_service io;
    printer p(io);
    //建立新线程调用io_service::run()
    boost::thread t(boost::bind(&boost::asio::io_service::run, &io));
    //当前线程调用io_service::run()
    io.run();
        //t.join()的作用是让主线程等待子线程结束
    t.join();
    
    
    getchar();
    return 0;
}

2 执行结果

多线程同步.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,519评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,174评论 19 139
  • 五一三天的假期,本打算看本书,学Objective-c,了解一下IOS开发。但总在拖延,把时间花在打游戏上。我明明...
    着实醉了的猫阅读 1,310评论 0 0
  • 既然选择了忘记 何必又忆起 原本死寂的春水 已泛动着涟漪 久久不能平静 …… ——J
    若洁阅读 1,139评论 0 1
  • 「逃离」这个词携带着慌乱狼狈之感,我当年去北京那会,也不是「奔向」的姿态,换到离开时,着实说不上逃避这般严重。很多...
    杨夏阅读 3,276评论 1 2

友情链接更多精彩内容