zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    PyQt5库结构
    PyQt5
    QObject
    os.listdir()
    os.getcwd()
    os.chdir()
    os.access()
    __name__
    php 学习笔记之关于时区的那点事
    php 学习笔记之搭建开发环境(mac版)
  • 原文地址:https://www.cnblogs.com/morya/p/10932752.html
Copyright © 2011-2022 走看看