美文网首页
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

    Ann. Oncol 丨 大数据分析免疫检查点阻断反应以提高治疗效果 原创珍奇图灵基因2022-09-13 10:...

  • 字慧乐园真有趣

    字慧乐园真有趣 原创 林老师 部编版大语文 2022-09-13 22:58 发表于福建 这节字慧乐园来了40位同...

  • 闲言碎语(7)(外三篇)

    1、《仲秋的乌镇》时间:2022-09-13 有一个地方叫乌镇,它是: ——典型的江南名镇。地处长三角杭嘉湖平原嘉...

  • 猫中极品(Marry和她的大白)

    2022-09-13 Marry是楼下我车位上的流浪猫,是一只母猫,是一只患了口炎非常厉害猫,2020年秋季来到这...

  • 0316|我是自己的教练试验品

    2022-09-13 北京 晴天一昨天是最后一天在上海,微风细雨,很浪漫的一天。大鱼,一个自信有才华的姑娘,诺言...

  • 收假返园

    2022-09-13 周二 阵雨 收假了,心心念念返园的反应没有我想象的严重。虽然心心从起床就一直强调不要上幼儿园...

  • 105|臭臭的新反应

    2022-09-13 星期二 雨 臭臭最近迷恋上喝妈妈的奶了,他妈妈一抱起来就开始找奶,找到了就拼命往上爬,真是够...

  • 2022-09-13

    这一个月痛苦并煎熬着

  • 2022-09-13

    [拥有贵相的人,懂得“未雨绸缪”] 《礼记·中庸》中有言: “凡事预则立,不预则废。” 意思是做任何事情,事前有准...

  • 2022-09-13

    1. 其实没什么好后悔的,很多事情就算时间能重来一遍,以当时的心智和阅历,还是会做同样的选择,避免不了同样的结果,...

网友评论

      本文标题:2022-09-13

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