您的位置:首页 > 大数据 > 人工智能

Boost.Aiso教程 5-同步多线程程序中的处理程序

2017-05-07 14:30 369 查看
Timer.5-同步多线程程序中的处理程序

#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>

class printer

{

public:

  printer(boost::asio::io_service& io)

    : strand_(io),

      timer1_(io, boost::posix_time::seconds(1)),

      timer2_(io, boost::posix_time::seconds(1)),

      count_(0)

  {

    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;

  }

  void print1()

  {

    if (count_ < 10)

    {

      std::cout << "Timer 1: " << count_ << 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_ << 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);

  boost::thread t(boost::bind(&boost::asio::io_service::run, &io));

  io.run();

  t.join();

  return 0;
}

本教程演示如何使用 boost::asio::strand 类来同步多线程程序中的回调处理程序。

以前的四个教程通过调用io_service::run()函数来从一个线程只能回避处理程序同步的问题。你已经知道,澳洲库提供了保证,只会从目前正在调用io_service::run()的线程调用处理程序的回调。因此,从只有一个线程调用io_service::run()确保回调处理程序不能同时运行。

单线程的方式通常是最好的地方,开始时使用澳洲开发应用程序。其缺点是它在程序,特别是服务器,包括放置的限制︰

可怜的响应时处理程序可以花很长的时间才能完成。

无法在多处理器系统上缩放。

如果你发现自己遇到这些限制,另一种方法是池中的某个线程调用io_service::run()。然而,这允许并发执行的处理程序,我们需要一种同步方法时处理程序可能正在访问一个共享的线程不安全的资源。

#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,类似于前面的教程中类类。此类将扩展前面的教程通过并行运行两个计时器。

class printer

{

public:

除了初始化一对 boost::asio::deadline_timer 成员,该构造函数初始化的strand_成员,对象类型 boost::asio::strand。

Boost::asio::strand 保证,对于通过它派出这些处理程序,将允许执行的处理程序完成之前开始下一个。这被保证,无论调用io_service::run()的线程数目。当然,处理程序可能仍然执行与其他处理程序不出动通过 boost::asio::strand 或出动通过不同的 boost::asio::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)

  {

当启动异步操作,每个回调处理程序是"包装"使用 boost::asio::strand 对象。钢绞线:: wrap()函数将返回一个新的处理程序,自动调度通过 boost::asio::strand 对象及其所包含的处理程序。通过包装使用相同的 boost::asio::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;

  }

在多线程程序中,如果他们访问的共享的资源,应同步异步操作的处理程序。在本教程中,处理程序 (print1和print2) 所使用的共享的资源是std::cout和count_数据成员。

  void print1()

  {

    if (count_ < 10)

    {

      std::cout << "Timer 1: " << count_ << 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_ << 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_;

};

main功能现在造成io_service::run()被称为从两个线程︰ 主线程和一个额外的线程。这被通过使用一个 boost::thread 对象。

就像它与从单一线程中调用,并发调用io_service::run()将继续执行剩下要做的"工作"时。后台线程不会退出直到所有异步操作已完成。

int main()

{

  boost::asio::io_service io;

  printer p(io);

  boost::thread t(boost::bind(&boost::asio::io_service::run, &io));

  io.run();

  t.join();

  return 0;

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  boost aiso