boost::asio (4): io_service_strand详解

先上参考文章:

1. http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting-started-with-boostasio/?pg=5

2. http://blog.csdn.net/huang_xw/article/details/8469851


下面的代码是我根据参考2修改的。

#include <boost/shared_ptr.hpp>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/shared_ptr.hpp>
#include <string>
#include <vector>
#include <list>
#include <iostream>
#include <boost/bind.hpp>
#include <boost/cstdint.hpp>
#include <boost/asio.hpp>
#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/condition.hpp>
using namespace boost;
using namespace std;

// strand提供串行执行, 能够保证线程安全, 同时被post或dispatch的方法, 不会被并发的执行.   
// io_service不能保证线程安全  
boost::asio::io_service m_service;  
boost::asio::strand m_strand(m_service);  
boost::mutex m_mutex;  

void print(int id)  
{  
    // boost::mutex::scoped_lock lock(m_mutex);  
    static int count = 0;  
    cout << "id: " << boost::lexical_cast<std::string>(id) << endl;
    cout << "count: " << boost::lexical_cast<std::string>(++count) << endl;

    //PRINT_DEBUG("id: " << boost::lexical_cast<std::string>(id));  
    //PRINT_DEBUG("count: " << boost::lexical_cast<std::string>(++count));  
}  

void ioRun1()  
{  
    while(true)  
    {  
        m_service.run();  
    }  
}  

void ioRun2()  
{  
    while(true)  
    {  
        m_service.run();  
    }  
}  

void strand_print1()  
{  
    // PRINT_DEBUG("Enter print1");  
    m_strand.dispatch(boost::bind(print, 1));  
    // PRINT_DEBUG("Exit print1");  
}  

void strand_print2()  
{  
    // PRINT_DEBUG("Enter print2");  
    m_strand.post(boost::bind(print, 2));  
    // PRINT_DEBUG("Exit print2");  
}  

void strand_print3()  
{  
    // PRINT_DEBUG("Enter print3");                
    m_strand.post(boost::bind(print, 3));  
    // PRINT_DEBUG("Exit print3");  
}  

void strand_print4()  
{  
    // PRINT_DEBUG("Enter print4");  
    m_strand.post(boost::bind(print, 4));  
    // PRINT_DEBUG("Exit print4");  
}  

// 将上面的m_strand换成m_service后,  
void service_print1()  
{  
    // PRINT_DEBUG("Enter print1");  
    m_service.dispatch(boost::bind(print, 1));  
    // PRINT_DEBUG("Exit print1");  
}  

void service_print2()  
{  
    // PRINT_DEBUG("Enter print2");  
    m_service.post(boost::bind(print, 2));  
    // PRINT_DEBUG("Exit print2");  
}  

void service_print3()  
{  
    // PRINT_DEBUG("Enter print3");                
    m_service.post(boost::bind(print, 3));  
    // PRINT_DEBUG("Exit print3");  
}  

void service_print4()  
{  
    // PRINT_DEBUG("Enter print4");  
    m_service.post(boost::bind(print, 4));  
    // PRINT_DEBUG("Exit print4");  
}  

void test_strand()  
{  
    boost::thread ios1(ioRun1);  
    boost::thread ios2(ioRun2);  

    boost::thread t1(strand_print1);  
    boost::thread t2(strand_print2);  
    boost::thread t3(strand_print3);  
    boost::thread t4(strand_print4);  

    t1.join();  
    t2.join();  
    t3.join();  
    t4.join();  

    m_service.run();  
}  

void test_service()  
{  
    boost::thread ios1(ioRun1);  
    boost::thread ios2(ioRun2);  

    boost::thread t1(service_print1);  
    boost::thread t2(service_print2);  
    boost::thread t3(service_print3);  
    boost::thread t4(service_print4);  

    t1.join();  
    t2.join();  
    t3.join();  
    t4.join();  

    m_service.run();  
}  

int main()
{
    test_service();
    //test_strand();

    ::system("PAUSE");
    
    return 0;
}

经过我的测试,

执行test_service()结果:

id: 1
count: 1
id: 2
id: 4
count: 3
count: 2
id: 3
count: 4

结果杂乱无章。


执行test_strand()结果:

id: 1
count: 1
id: 2
count: 2
id: 4
count: 3
id: 3
count: 4
可见test_strand()中的id与count是成对出现的。

由此验证了上述bolg的结果。



你可能感兴趣的:(boost::asio (4): io_service_strand详解)