c++ boost Exector

原创
2020/03/29 12:36
阅读数 300

前言

        在应用开发中经常要执行一些异步的函数,有些是没有返回结果,有些是有返回结果,甚至有些是定时的任务,本文在boost io_service基础上搭建一个Exector来执行这些任务

 

代码

#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include <iostream>
#include <unistd.h>
#include <boost/function.hpp>
#include <vector>


class Executor {

public:
    Executor(int count) {
        _io_service = boost::make_shared<boost::asio::io_service>();
        _work = boost::make_shared<boost::asio::io_service::work>(*_io_service);
        for (int i = 0; i < count; ++i) {
            boost::shared_ptr<boost::thread> thread = boost::shared_ptr<boost::thread>(
                    new boost::thread(boost::bind(&boost::asio::io_service::run, _io_service)));
            _threads.push_back(thread);
        }
    }

    ~Executor() {
    }

    void postTask(boost::function<void()> function) {
        _io_service->post(function);
    }

    void postTimerTaskSecond(boost::function<void()> function, int second = 3) {
        boost::posix_time::seconds interval(second);
        boost::shared_ptr<boost::asio::deadline_timer> timer = boost::shared_ptr<boost::asio::deadline_timer>(
                new boost::asio::deadline_timer(*(_io_service), interval));
        timer->async_wait([function, timer](const boost::system::error_code &) {
            function();
        });
    }

    void postTimerTaskMilliSecond(boost::function<void()> function, int microsecond = 3) {
        boost::posix_time::millisec interval(microsecond);
        boost::shared_ptr<boost::asio::deadline_timer> timer = boost::shared_ptr<boost::asio::deadline_timer>(
                new boost::asio::deadline_timer(*(_io_service), interval));
        timer->async_wait([function, timer](const boost::system::error_code &) {
            function();
        });
    }

    template<class T>
    boost::shared_future<T> postTask(boost::function<T()> function) {
        boost::shared_ptr<boost::packaged_task<T>> task = boost::shared_ptr<boost::packaged_task<T>>(
                new boost::packaged_task<T>(function));
        boost::shared_future<T> fut(task->get_future());
        _io_service->post(boost::bind(&boost::packaged_task<T>::operator(), task));
        return fut;
    }

public:
    boost::shared_ptr<boost::asio::io_service> _io_service;
    boost::shared_ptr<boost::asio::io_service::work> _work;
    std::vector<boost::shared_ptr<boost::thread >> _threads;
};

class API {
public:
    void call(int a) {
        std::cout << "a=" << a << "  call \n";
    }

    double call(int a, int b) {
        std::cout << " a=" << a << " b=" << b << "\n";
        return 11.11;
    }

    void timer() {
        std::cout << "tick" << std::endl;
    }
};

int main() {
    Executor executor(3);
    boost::shared_ptr<API> api = boost::make_shared<API>();
    executor.postTimerTaskSecond([]() {
        std::cout << "tick1" << std::endl;
    }, 1);
    executor.postTimerTaskSecond([]() {
        std::cout << "tick2" << std::endl;
    }, 2);
    executor.postTimerTaskSecond([]() {
        std::cout << "tick3" << std::endl;
    }, 3);
    executor.postTimerTaskMilliSecond([]() {
        std::cout << "tick4" << std::endl;
    }, 4000);
    executor.postTimerTaskMilliSecond([]() {
        std::cout << "tick5" << std::endl;
    }, 5000);

    executor.postTask([]() {
        std::cout << std::this_thread::get_id() << "  print e \n";
    });

    auto fut = executor.postTask<double>([]() -> double {
        return 111.11;
    });
    std::cout << fut.get() << "  \n";

    auto f = executor.postTask<double>(boost::bind(&API::call, api, 1, 2));
    std::cout << f.get() << "\n";
    sleep(6);
    return 0;
}

 

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部