C++boost之asio strand

1 篇文章 0 订阅
1 篇文章 0 订阅

以c++ boost 1.66版本为例,类boost::asio::io_context::strand的主要作用是在asio中利用多线程进行事件处理的时候,如果涉及到多线程访问共享资源,借助于strand类,我们不需要显示的使用线程同步相关的类(比如mutex)就可以让多个事件处理函数依次执行。

简而言之,strand定义了事件处理程序的严格顺序调用。

我们知道,若多个线程调用了同一个io_context对象的run方法,那么该对象关联的多个事件处理函数 可能就会被不同的线程同时执行(即并发执行),若这些事件处理函数访问同一个非线程安全的共享资源时,就可能会产生线程同步问题。 但是若我们将这些事件处理函数bind到同一个strand对象上,那么asio库保证在上一个事件处理函数处理完成之前是没法执行下一个事件处理函数的(相当于阻止了并发执行)。

下面以deadline_timer调用为例

#include <iostream>
#include <thread>
//简单日志宏,打印日志及附加的线程ID
#define  LOG(a) std::cout<<"[T"<<std::this_thread::get_id()<<"]"<<a<<"\n"

#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include<boost/bind.hpp>
#include<boost/asio/strand.hpp>


class Printer
{
public:
	Printer(boost::asio::io_context& ioc)
		:m_strand(ioc), 
		m_timer1(ioc, boost::posix_time::seconds(1)),
		m_timer2(ioc, boost::posix_time::seconds(1))
	{
		m_timer1.async_wait(boost::asio::bind_executor(m_strand, boost::bind(&Printer::print1, this)));
		m_timer2.async_wait(boost::asio::bind_executor(m_strand, boost::bind(&Printer::print2, this)));
		//m_timer1.async_wait(boost::bind(&Printer::print1, this));
		//m_timer2.async_wait(boost::bind(&Printer::print2, this));
	}

	void print1()
	{
		if (m_count++< 10)
		{
			LOG("print 1 ...count "<<m_count);
			m_timer1.expires_at(m_timer1.expires_at() + boost::posix_time::seconds(1));
			m_timer1.async_wait(boost::asio::bind_executor(m_strand, boost::bind(&Printer::print1, this)));
			//m_timer1.async_wait(boost::bind(&Printer::print1, this));
		}
	}
	void print2()
	{
		if (m_count++ < 10)
		{
			LOG("print 2 ...count "<< m_count);
			m_timer2.expires_at(m_timer2.expires_at() + boost::posix_time::seconds(1));
			m_timer2.async_wait(boost::asio::bind_executor(m_strand, boost::bind(&Printer::print2, this)));
			//m_timer2.async_wait(boost::bind(&Printer::print2, this));
		}
	}
private:
	boost::asio::io_context::strand m_strand;
	boost::asio::deadline_timer m_timer1;
	boost::asio::deadline_timer m_timer2;
	int m_count = 0;
};


int main()
{
	boost::asio::io_context ioc;
	Printer printer(ioc);
	auto beg = boost::posix_time::second_clock::local_time();
	std::thread th([&ioc]() {
		ioc.run();
	});
	ioc.run();
	th.join();
	auto end = boost::posix_time::second_clock::local_time();
	LOG("cost " << (end - beg).total_seconds() << " (s)");
	return 0;
}

Printer类中的两个dealine_timer对象每隔1秒等待一次超时事件,两者相加总共10次;和m_strand一样,他们都通过相同的ioc对象来构造;我们利用boost::asio::bind_executor分别将两个timer对象的超时处理函数绑定到同一个strand对象上来;所以就算在main函数中有两个线程都调用ioc的run方法,在处理函数实际执行的时候还是依次执行的。

输出结果:


相反,若我们绕过strand对象,直接将 事件处理函数绑定到超时事件上,那么可能会产生的输出结果如下:

 

  • 11
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
Boost.AsioBoost 库中的一个模块,提供了 C++ 编程语言的异步 I/O 和网络编程功能。它是一个跨平台的库,支持多种操作系统和网络协议。 Boost.Asio 提供了以下主要功能: 1. 异步 I/O 操作:Boost.Asio 提供了异步操作的支持,可以在程序中执行非阻塞的 I/O 操作,如读取和写入文件、套接字和串口等。通过异步操作,可以充分利用系统资源,提高程序的并发性和响应性。 2. 网络编程:Boost.Asio 提供了丰富的网络编程功能,包括 TCP、UDP、SSL 等协议的支持。可以使用 Boost.Asio 构建服务器和客户端应用程序,处理网络连接、数据传输和处理等。 3. 定时器和定时事件:Boost.Asio 提供了定时器和定时事件的支持,可以实现定时执行任务、周期性任务和超时处理等功能。 4. 可移植性:Boost.Asio 是一个跨平台的库,可以在多种操作系统上使用,如 Windows、Linux、macOS 等。它还支持 IPv4 和 IPv6 网络协议,并提供了对套接字选项、网络接口和地址等的操作。 使用 Boost.Asio 需要先安装 Boost 库,并在编译时链接相应的库文件。以下是一个简单的示例代码,演示了 Boost.Asio 的用法: ```cpp #include <iostream> #include <boost/asio.hpp> int main() { boost::asio::io_context io_context; // 创建一个 I/O 上下文 boost::asio::ip::tcp::socket socket(io_context); // 创建一个 TCP 套接字 boost::system::error_code ec; socket.connect(boost::asio::ip::tcp::endpoint(boost::asio::ip::address::from_string("127.0.0.1"), 1234), ec); // 连接到指定的服务器端口 if (ec) { std::cout << "连接失败:" << ec.message() << std::endl; } else { std::cout << "连接成功!" << std::endl; } return 0; } ``` 在上述示例中,我们使用 Boost.Asio 创建了一个 TCP 套接字,并尝试连接到本地的 1234 端口。通过使用 Boost.Asio 提供的类和函数,我们可以轻松地进行网络编程和异步 I/O 操作。 需要注意的是,Boost.Asio 提供了更多丰富的功能和用法,如异步读写操作、服务器编程、多线程和多线程池等。你可以参考 Boost.Asio 的官方文档和示例代码,以了解更多详细信息和用法示例。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值