Asio_3/4

参考来源https://www.gamedev.net/blogs/entry/2249317-a-guide-to-getting-started-with-boostasio/

Example 3a

asio简单运行使用:

#include 
#include 
#include 
#include 
#include
#include 
#include 

std::mutex global_lock;

void workerThread(std::shared_ptr io_service ){
    global_lock.lock();
    std::cout<run();
    global_lock.lock();
    std::cout< ntid;
    for(int i=0;i<2;i++)
    {
        ntid.push_back(std::thread(workerThread,io_service));
    }

    io_service->post(std::bind(CalculateFib,3));
    io_service->post(std::bind(CalculateFib,4));
    io_service->post(std::bind(CalculateFib,5));
    std::cin.get();
    io_service->stop();
    for(auto &iter:ntid){
        iter.join();
    }
    return 0;

}

结果如下:


image.png

Example 3b

post()和dispatch()的区别:Dispatched events can execute from the current worker thread even if there are other pending events queued up. The posted events have to wait until the handler completes before being allowed to be executed.

#include 
#include 
#include 
#include 
#include
#include 
#include 

std::mutex global_lock;

void workerThread(std::shared_ptr io_service ){
    global_lock.lock();
    std::cout<run();
    global_lock.lock();
    std::cout<io_service){
    for(int i=0;i<4;i++){
        io_service->dispatch(std::bind(dispatch,i*2));
        io_service->post(std::bind(post,i*2+1));
        std::this_thread::sleep_for(std::chrono::milliseconds(1000));
    }
}
int main(int argc,char * argv[]){
    std::shared_ptr io_service(
        new asio::io_service);
    std::shared_ptr work(
    new asio::io_service::work(*io_service));
    std::cout<<"Press Enter to exit"< ntid;
    for(int i=0;i<1;i++)
    {
        ntid.push_back(std::thread(workerThread,io_service));
    }

    io_service->post(std::bind(run,io_service));
    std::cin.get();
    io_service->stop();
    for(auto &iter:ntid){
        iter.join();
    }
    return 0;

}

结果如下:


image.png

Example 4a

strand定义了事件处理程序的严格顺序调用!This is because the strand object is correctly serializing the event processing to only one thread at a time. It is very important that we notice that strand does not serialize work through only one thread either.

#include 
#include 
#include 
#include 
#include
#include 
#include 

std::mutex global_lock;

void workerThread(std::shared_ptr io_service ){
    global_lock.lock();
    std::cout<run();
    global_lock.lock();
    std::cout< io_service(
        new asio::io_service);
    std::shared_ptr work(
    new asio::io_service::work(*io_service));

    asio::io_service::strand strand(*io_service);
    std::cout<<"Press Enter to exit"< ntid;
    for(int i=0;i<2;i++)
    {
        ntid.push_back(std::thread(workerThread,io_service));
    }

    std::this_thread::sleep_for(std::chrono::milliseconds(1000));
    for(int i=0;i<50;i++){
        //io_service->post(std::bind(printNum,i));  //结果如图1所示
        strand.post(std::bind(printNum,i));            //结果如图2所示
    }
    std::cin.get();
    io_service->stop();
    for(auto &iter:ntid){
        iter.join();
    }
    return 0;

}
图1.png
图2.png

Example 4b

如果采用wrap包装函数,会有如下现象。--This is because the work we are passing is guaranteed to be executed serially, but there is no guarantee to which the order of the work actually takes place as a result of the API functions we are using!

#include 
#include 
#include 
#include 
#include
#include 
#include 

std::mutex global_lock;

void workerThread(std::shared_ptr io_service ){
    global_lock.lock();
    std::cout<run();
    global_lock.lock();
    std::cout< io_service(
        new asio::io_service);
    std::shared_ptr work(
    new asio::io_service::work(*io_service));

    asio::io_service::strand strand(*io_service);
    std::cout<<"Press Enter to exit"< ntid;
    for(int i=0;i<4;i++)
    {
        ntid.push_back(std::thread(workerThread,io_service));
    }

    
    std::this_thread::sleep_for(std::chrono::milliseconds(100));
    io_service->post(strand.wrap(std::bind(printNum,1)));
    io_service->post(strand.wrap(std::bind(printNum,2)));

    std::this_thread::sleep_for(std::chrono::milliseconds(100));
    io_service->post(strand.wrap(std::bind(printNum,3)));
    io_service->post(strand.wrap(std::bind(printNum,4)));

    std::this_thread::sleep_for(std::chrono::milliseconds(100));
    io_service->post(strand.wrap(std::bind(printNum,5)));
    io_service->post(strand.wrap(std::bind(printNum,6)));
    std::cin.get();
    io_service->stop();
    for(auto &iter:ntid){
        iter.join();
    }
    return 0;

}
image.png

你可能感兴趣的:(Asio_3/4)