Asio-6定时器

Example 6a

简单使用

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

std::mutex global_lock;

void workerThread(std::shared_ptr io_service ){
    global_lock.lock();
    std::cout<run(ec);

        if(ec){
            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));
    }

    asio::steady_timer timer(*io_service);
    timer.expires_from_now(asio::chrono::seconds(5));
    timer.async_wait(timerHandle);
    //io_service->post(std::bind(raiseException,io_service));   
    std::cin.get();
    io_service->stop();
    for(auto &iter:ntid){
        iter.join();
    }
    return 0;

}
image.png

Example 6b

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

std::mutex global_lock;

void workerThread(std::shared_ptr io_service ){
    global_lock.lock();
    std::cout<run(ec);

        if(ec){
            global_lock.lock();
            std::cout< timer){
    if(error){
        global_lock.lock();
        std::cout<expires_from_now(asio::chrono::seconds(1));
        timer->async_wait(std::bind(timerHandle,std::placeholders::_1,timer));
    }
}

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));

//  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::shared_ptr timer=std::make_shared(*io_service);
    timer->expires_from_now(asio::chrono::seconds(5));
    timer->async_wait(std::bind(timerHandle,std::placeholders::_1,timer));
    //io_service->post(std::bind(raiseException,io_service));   
    std::cin.get();
    io_service->stop();
    for(auto &iter:ntid){
        iter.join();
    }
    return 0;

}
image.png

Example 6c

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

std::mutex global_lock;

void workerThread(std::shared_ptr io_service ){
    global_lock.lock();
    std::cout<run(ec);

        if(ec){
            global_lock.lock();
            std::cout< timer,std::shared_ptr strand){
    if(error){
        global_lock.lock();
        std::cout<expires_from_now(asio::chrono::seconds(1));
        timer->async_wait(strand->wrap(std::bind(timerHandle,std::placeholders::_1,timer,strand)));
    }
}
void printNum(int x){

        std::cout<<"["< io_service(
        new asio::io_service);
    std::shared_ptr work(
    new asio::io_service::work(*io_service));

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

    strand->post(std::bind(printNum,1));
    strand->post(std::bind(printNum,2));
    strand->post(std::bind(printNum,3));
    strand->post(std::bind(printNum,4));

    std::shared_ptr timer=std::make_shared(*io_service);
    timer->expires_from_now(asio::chrono::seconds(5));
    timer->async_wait(strand->wrap(std::bind(timerHandle,std::placeholders::_1,timer,strand)));
    //io_service->post(std::bind(raiseException,io_service));   
    std::cin.get();
    io_service->stop();
    for(auto &iter:ntid){
        iter.join();
    }
    return 0;

}
image.png

你可能感兴趣的:(Asio-6定时器)