加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
HeapTimer.h 1.30 KB
一键复制 编辑 原始数据 按行查看 历史
“hyk” 提交于 2021-07-22 01:01 . change m_status to atomic
#pragma once
#include "TimeNode.h"
#include <unordered_map>
#include <vector>
#include <unistd.h>
#include <pthread.h>
#include <iostream>
#define IS_LESS_STAMP(i,j) (m_container[(i)]->timestamp<m_container[(j)]->timestamp)
#define C_SIZE (m_container.size())
//precise to second
class HeapTimer
{
public:
enum TimerManagerState
{
TIME_MANAGER_START,
TIME_MANAGER_STOP
};
public:
~HeapTimer();
//this example prove only one timer
static HeapTimer* instance();
int addTimer(TimeNode* newnode) ;
void deleteTimer(int id) ;
void clear();
void start();
void stop();
private:
static void* process(void* arg);
explicit HeapTimer(/* args */);
private:
//try minheap
void sinkdown(int index);//for delete
void popup(int index);//for insert
void insertTimeNode(TimeNode* newnode);
void deleteTimeNode(TimeNode* node);
void swapTimeNode(int i,int j);
TimeNode* front();
private:
//vector is more comfortable
std::vector<TimeNode*> m_container;
//log(1) to find node id->TimeNode
std::unordered_map<int,TimeNode*> m_node_location;
static std::atomic<HeapTimer*> smp_heaptimer;
static std::atomic<TimerManagerState> m_status;//pthread is runing or not
static pthread_mutex_t sm_mutex;
pthread_t m_pid;
};
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化