美文网首页
2022-09-13

2022-09-13

作者: Tomasmule | 来源:发表于2022-09-13 14:32 被阅读0次
    int main(int argc, const char *argv[])
    {
        double res = AddNumbers(1.0, 2.0);
        printf("%lf", res);
        return 0;
    }
    
    #include <iostream>
    #include <string>
    #include <queue>
    #include <thread>
    #include <chrono>
    using namespace std;
    using namespace chrono;
    
    struct Order{
        string id;
        string name;
        int preTime; // second
        int64_t time; // finish time
    
        Order() {
            preTime = 0;
            time = 0;
            id = "";
            name = "";
        }
        Order(Order *order) {
            this->id = order->id;
            this->name = order->name;
            this->preTime = order->preTime;
            this->time = order->time;
        }
        bool operator < (const Order &od) const
        {
            return time < od.time; 
        }
        bool operator > (const Order &od) const
        {
            return time > od.time;
        }
    };
    
    class MyQueue {
    public:
        virtual int Init(int cap) = 0;
        virtual void Deinit() = 0;
        virtual int Push(Order order) = 0;
        virtual Order GetFrontAndPop(void) = 0;
        virtual bool IsFull(void) = 0;
        virtual bool IsEmpty(void) = 0;
    };
    
    class Queue : public MyQueue {
    public:
        int Init(int cap)
        {
            this->capicity = cap;
            pthread_mutex_init(&(this->mutex), NULL);
        }
        void Deinit()
        {
            // lock , free order
            pthread_mutex_lock(&(this->mutex));
            while (!this->que.empty()) {
                this->que.pop();
            }
    
            pthread_mutex_unlock(&(this->mutex));
    
            //
            pthread_mutex_destroy(&(this->mutex));
            return;
        }
    
        int Push(Order order)
        {
            cout<<"input order id: "<<order.id<<endl;
            if (this->que.size() >= this->capicity) {
                return -1;
            }
            this->que.push(order);
            return 0;
        }
        bool IsFull() {
            return this->que.size() == this->capicity;
        }
        bool IsEmpty() {
            return this->que.empty();
        }
        Order GetFrontAndPop() {
            while (this->que.empty()) {
                // error
                std::this_thread::sleep_for(1000ms);
                cout<<"que is empty\n";
            }
            Order order = this->que.front();
            this->que.pop();
            return order;
        }
    private:
        int capicity;
        queue<Order> que;
        pthread_mutex_t mutex;
    };
    class ProQueue : public MyQueue {
    public:
        int Init(int cap)
        {
            this->capicity = cap;
            pthread_mutex_init(&(this->mutex), NULL);
        }
        void Deinit()
        {
            // lock , free order
            pthread_mutex_lock(&(this->mutex));
            while (!this->que.empty()) {
                this->que.pop();
            }
    
            pthread_mutex_unlock(&(this->mutex));
    
            //
            pthread_mutex_destroy(&(this->mutex));
            return;
        }
    
        int Push(Order order)
        {
            if (this->que.size() >= this->capicity) {
                return -1;
            }
            this->que.push(order);
            return 0;
        }
        bool IsFull() {
            return this->que.size() == this->capicity;
        }
        bool IsEmpty() {
            return this->que.empty();
        }
        Order GetFrontAndPop() {
            while (this->que.empty()) {
                // error
                std::this_thread::sleep_for(1000ms);
                cout<<"que is empty\n";
            }
            Order order = this->que.top();
            this->que.pop();
            return order;
        }
    private:
        int capicity;
        priority_queue<Order, vector<Order>, greater<Order>> que;
        pthread_mutex_t mutex;
    };
    
    class Input {
    public:
        int Product(MyQueue *myque, void *args)
        {
            Order *order = (Order*)args;
            Order od(order); 
            int ret = myque->Push(order);
            return ret;
        }
    };
    
    class Dispatch {
    public: 
        virtual int DispatchOrder(MyQueue *que) = 0;
        int CariesArrivedTime(int min, int max)
        {
            // rand from min to max, [min, max]
    
        }
        void AddFoodWait(int preTime)
        {
            this->foodwait += preTime;
        }
        int64_t GetFoodWait()
        {
            return foodwait;
        }
        void AddCariesWait(int wait)
        {
            this->carieswait += wait;
        }
        int64_t GetCariesWait()
        {
            return carieswait;
        }
    
    private:
        int64_t foodwait;
        int64_t carieswait;
        int64_t orders;
    };
    
    class Matched : public Dispatch
    {
    public:
        int DispatchOrder(MyQueue *que)
        {
            Queue *q = (Queue*)que;
            
            Order order = q->GetFrontAndPop();
    
            AddFoodWait(order.preTime);
            cout<<"Matched Dispatch "<<order.name<<" "<<order.preTime<<" "<<order.time<<endl;
    
    
        }
    };
    
    class FIFO : public Dispatch
    {
    public:
        int DispatchOrder(MyQueue *que)
        {
            ProQueue *q = (ProQueue*)que;
            Order order = q->GetFrontAndPop();
            auto now = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
            if (now < order.time) {
                q->Push(order);
                return -2;
            }
            cout<<"Match Dispatch "<<order.name<<" "<<order.preTime<<" "<<order.time<<endl;
            return 0;
        }
    
    };
    
    int DispatchMeth(MyQueue *que, Dispatch *dispatch)
    {
        while (true) {
            if (que->IsEmpty()) {
                std::this_thread::sleep_for(1000ms);
                cout<<std::this_thread::get_id()<<"order is emtpy, sleep 1 s\n";
                continue;
            }
            if (dispatch->DispatchOrder(que) == -2) { // order not prepared
                std::this_thread::sleep_for(200ms);
                // cout<<std::this_thread::get_id()<<"order is not prepare, sleep 200 ms\n";
            }
        }
    }
    
    
    // 断续读取文件
    int Read(const char* file, MyQueue *que)
    {
        auto start = system_clock::now();
        auto end = system_clock::now();
        while (true) {
            std::this_thread::sleep_for(2000ms);
            auto end = system_clock::now();
            std::chrono::duration<double, std::milli> elapsed = end-start;
            //cout<<"read frome file: "<<file<<" tims: "<<elapsed.count()<<" ms"<<endl;
            while (que->IsFull()) {
                std::this_thread::sleep_for(1000ms);
                cout<<std::this_thread::get_id()<< "que is full\n";
            }
            static int64_t id = 0;
            for (int i = 0; i < 2; i++) {
                Order order;
                order.name = "rndNO" + to_string(id) + "*";
                order.id = to_string(id);
                id++;
                order.preTime = rand() % 15;
                order.time = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count() + order.preTime * 1000;
                que->Push(&order);
            }
        }
    }
    
    
    int main()
    {
    /*     Queue myque;
        myque.Init(10);
        Input input;
        //myque.Init(100);
        Matched match;
    
        thread read(Read, "/home/err", &myque);
        thread disp(DispatchMeth, &myque, &match);
        read.join();
        disp.join(); */
    
        ProQueue proque;
        proque.Init(10);
        FIFO fifo;
    
        thread read(Read, "/home/err", &proque);
        thread disp(DispatchMeth, &proque, &fifo);
        read.join();
        disp.join();
    
    
        cout<<"hello world\n";
    }
    
    
    

    cmake_minimum_required(VERSION 3.16)
    project(SERVER)

    include_directories(
    ${CMAKE_CURRENT_SOURCE_DIR}/include
    )
    aux_source_directory(. DIR_SRCS)

    add_executable(Server ${DIR_SRCS})

    target_link_libraries(Server PUBLIC pthread)

    
    

    相关文章

      网友评论

          本文标题:2022-09-13

          本文链接:https://www.haomeiwen.com/subject/jphhortx.html