以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对象,直接将 事件处理函数绑定到超时事件上,那么可能会产生的输出结果如下: