• C++11 thread condition_variable mutex 综合使用


    #include <mutex>
    #include <condition_variable>
    #include <chrono>
    #include <thread>
    #include <glog/logging.h>
    
    class Event {
    public:
        Event();
        ~Event();
    
        bool wait(std::chrono::milliseconds millisec);
        void notify();
    
    private:
        std::mutex m_lock;
        std::condition_variable m_cond;
    };
    
    Event::Event() {
    }
    
    Event::~Event() {
    }
    
    bool Event::wait(std::chrono::milliseconds millisec) {
        LOG(INFO)<< "before lock";
        std::unique_lock<std::mutex> l(m_lock);
        auto cv = m_cond.wait_for(l, millisec);
        if (cv == std::cv_status::no_timeout) {
            return true;
        }
        return false;
    }
    
    void Event::notify() {
        m_cond.notify_all();
    }
    
    class App {
    public:
        App();
        ~App();
    
        bool start();
        static void threadRun(void * p);
        void run();
        void stop();
        void postExit();
    
    private:
        Event m_event;
        std::mutex m_lock;
        std::thread * m_thread;
    };
    
    App::App() {
        m_thread = NULL;
    }
    
    App::~App() {
        m_thread = NULL;
    }
    
    bool App::start() {
        std::unique_lock<std::mutex> l(m_lock);
        if (m_thread != NULL) {
            LOG(INFO)<<"thread running";
            return false;
        }
        m_thread = new std::thread(threadRun, this);
        if (m_thread == NULL) {
            LOG(INFO)<<"create thread failed";
            return false;
        }
        LOG(INFO)<< "create thread success";
        return true;
    }
    
    void App::threadRun(void *p) {
        App * pThis = (App*) p;
        pThis->run();
    }
    
    void App::run() {
        while (!this->m_event.wait(std::chrono::milliseconds(1002))) {
            LOG(INFO)<< "sleep";
        }
    }
    
    void App::postExit() {
        delete this->m_thread;
        this->m_thread = NULL;
    }
    
    void App::stop() {
        std::unique_lock<std::mutex> l(m_lock);
        this->m_event.notify();
        this->m_thread->join();
        postExit();
    }
    
    int main(int ac, char**av) {
        google::InitGoogleLogging(av[0]);
    
        FLAGS_alsologtostderr = true;
        FLAGS_logtostderr = true;
    
        LOG(INFO)<< "app started";
    
        std::unique_ptr<App> p(new App);
        if (!p->start()) {
            LOG(INFO)<< "start failed";
            return 1;
        }
        for (auto i = 0; i < 3; i++) {
            std::this_thread::sleep_for(std::chrono::seconds(2));
            LOG(INFO)<<"main thread sleep";
        }
        p->stop();
    
        LOG(INFO)<< "main thread done";
        return 0;
    }
    
  • 相关阅读:
    其他内容
    html标签
    ambari安装集群下安装kafka manager
    greenplum-cc-web4.0监控安装
    ambari安装集群下python连接hbase之安装thrift
    hadoop运维问题记录
    Ambari2.6.0 安装HDP2.6.3(离线安装)
    mongodb 定时备份
    linux top命令详解
    Sublime Text3配置Python环境
  • 原文地址:https://www.cnblogs.com/morya/p/10932752.html
Copyright © 2020-2023  润新知