Boost:asio strand同步

asio当有多个线程调用了run,那么异步任务会被分发到不同的线程中同时执行,如果多个线程访问相同的资源,有可能会造成竞争:


#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace boost::asio;
using namespace std;
 
static atomic_int s_timerNum = 0;
static int s_num = 0;
 
void timer_handler(boost::asio::deadline_timer* timer, const boost::system::error_code& err);
 
void timer_enable(boost::asio::deadline_timer* timer, size_t time_long = 1)    
{
    s_timerNum++;
    timer->expires_from_now(boost::posix_time::microseconds(time_long));
    auto f = bind(timer_handler, timer, std::placeholders::_1);
    timer->async_wait(f);
}
 
void timer_handler(boost::asio::deadline_timer* timer, const boost::system::error_code& err)
{
    if (err)
    {
        cout <<

你可能感兴趣的:(#,Boost,c++)