1 Star 0 Fork 0

huangchengcheng / class_TimeWheel

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
TimeWheel.cpp 3.48 KB
一键复制 编辑 原始数据 按行查看 历史
huangchengcheng 提交于 2021-06-20 15:06 . over
#include <time.h>
#include <string>
#include <pthread.h>
#include <unistd.h> //sleep function
#include <stdlib.h>
#include <iostream>
using namespace std;
struct hased_wheel_timeout{
int round;
int slot_num;
string msg;
int delay_time;
time_t start_time;
struct hased_wheel_timeout *next = NULL;
};
class TimeWheel{
public:
int task_num = 0;
time_t start_time;
static const int N = 10;
struct hased_wheel_timeout *slots[N], *pre_task, *next_task; //This is important
struct hased_wheel_timeout task;
TimeWheel(){
start_time = time(NULL);
for(int i = 0; i < N; i++){
slots[i] = (struct hased_wheel_timeout*)malloc(sizeof(struct hased_wheel_timeout));
}
}
~TimeWheel(){
for(int i = 0; i < N; i++){
struct hased_wheel_timeout* tmp = slots[i];
while(tmp -> next != NULL){
struct hased_wheel_timeout* _free = tmp;
tmp = tmp->next;
free(_free);
}
}
}
void add_task(int delay_time, string s){
time_t cur_time = time(NULL);
time_t dead_time = cur_time + delay_time;// - start_time;
int slot_num = ((long)dead_time) % N;
int round = ((long)dead_time) / N;
struct hased_wheel_timeout* task = (struct hased_wheel_timeout *)malloc(sizeof(struct hased_wheel_timeout));
task -> delay_time = delay_time;
task -> msg = s;
task -> round = round + 1; //for > 0
task -> slot_num = slot_num;
task -> start_time = cur_time;
task -> next = NULL;
cout<<"Add in slot : "<<slot_num<<endl;
struct hased_wheel_timeout* tmp = slots[slot_num];
while(tmp -> next != NULL){
tmp = tmp -> next;
}
tmp -> next = task;
task_num++;
}
void del_task(struct hased_wheel_timeout *pre, struct hased_wheel_timeout *now){
pre -> next = now -> next;
free(now);
}
//the relationship between add_task and run
bool run(){
while(task_num){
time_t cur_time = time(NULL);
int slot_num = ((long)cur_time) % N;
// cout<<"Now in slot : "<<slot_num<<endl;
struct hased_wheel_timeout* point = slots[slot_num];
pre_task = point;
point = point -> next;
while(point != NULL){
point -> round = point -> round - 1;
if(point -> round <= 0){
cout<<"There is a clock for you:"<<endl;
cout<<" "<<point -> msg <<endl;
pre_task -> next = point -> next;
free(point);
point = pre_task ->next;
task_num--;
}else{
pre_task = point;
point = point -> next;
}
}
}
return false;
}
};
struct Msg{
TimeWheel wheel;
int task_num;
};
void* check_time(void *arg){
struct Msg *msg = (struct Msg *)arg;
TimeWheel wheel = msg -> wheel;
int num = msg -> task_num;
while(wheel.run()){
sleep(1);
}
}
int main(){
TimeWheel wheel;
wheel.add_task(2, "This is 2 second message!");
wheel.add_task(10, "This is 10 second message!");
wheel.add_task(5, "This is 5 second message!");
pthread_t thread_handle;
struct Msg msg;
msg.wheel = wheel;
msg.task_num = 3;
pthread_create(&thread_handle, NULL, check_time, &msg);
pthread_join(thread_handle, NULL);
cout<<"pthread id : " << thread_handle <<endl;
}
1
https://gitee.com/huangchengcheng98/class_-time-wheel.git
git@gitee.com:huangchengcheng98/class_-time-wheel.git
huangchengcheng98
class_-time-wheel
class_TimeWheel
master

搜索帮助