workThread.h 1.7 KB
Newer Older
D
dolphin8 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
#pragma once

#include <thread>
#include <condition_variable>
#include <mutex>
#include <iostream>
// #include <function>
// #include <queue>

template <typename T>
class workThread {
public:
    void init(std::vector<T> & ops) {
        this->ops = ops;
        t = std::thread([this]{
            threadLoop();
        });
    }
    void threadLoop() {
        //std::unique_lock<std::mutex> lk(m);
        for (;;) {
            while(true) {
                std::lock_guard<std::mutex> lg(m);
                if (job != -1) break;
            };
            // std::cout << "loop!" << "\n";
            // cv.wait(lk, [this]{
            //     // std::cout << "wake up!!! you have work to do: " << job << "\n";
            //     return job != -1;
            // });
            if (job == -2) {
                break;
            }
            // TODO run job
            ops[job]->Run();
            job = -1;
            cv.notify_one();
        }
    }
    void run(int i) {
        std::lock_guard<std::mutex> lk(m);
        job = i;
        cv.notify_one();
    }
    void join() {
        std::unique_lock<std::mutex> lk(m);
        cv.wait(lk, [this]{
            return job == -1;
        });
    }
    void quit() {
        {
            std::cout << "try get lock!!!\n";
            std::lock_guard<std::mutex> lk(m);
            std::cout << "set job = -2\n";
            job = -2;
            cv.notify_one();
        }
        t.join();
        std::cout << "job quit\n";
    }
private:
    std::vector<T> ops;
    // std::queue<std::function<void()>> q;
    int job = -1;
    // std::atomic<int> job;
    std::condition_variable cv;
    std::mutex m;
    std::thread t;
    // bool idle = false;
};