2009 March 12th Thursday (三月 十二日 木曜日)
来源:互联网 发布:ntfs for mac 破解版 编辑:程序博客网 时间:2024/05/11 10:15
The old event queue demo has a few bugs. I revised them. The following is new version.
// evt.cpp
#include <time.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
#define SIG_ABRT 1
#define SIG_STOP 2
#define SIG_KILL 3
#define SIG_CHLD 4
#define SIG_MAX 5
struct _EvtEntry {
int event;
struct _EvtEntry *next;
};
typedef struct _EvtEntry EvtEntry;
class EvtQueue {
private:
int event_cnt; // the count events.
EvtEntry *head;
EvtEntry *tail;
public:
EvtQueue();
~EvtQueue();
void AppendEvt(int evt);
void AddEvt(int evt);
int RemoveEvt();
int getEvtCnt();
};
EvtQueue::EvtQueue(){
event_cnt = 0;
head = NULL;
tail = NULL;
}
EvtQueue::~EvtQueue() {
if (event_cnt > 0) {
int cnt = event_cnt;
for (int i = 0; i < cnt; i++)
RemoveEvt();
head = NULL;
tail = NULL;
event_cnt = 0;
}
}
void EvtQueue::AppendEvt(int evt) {
EvtEntry *pEvt = new EvtEntry();
if (pEvt) {
// initialization
pEvt->event = evt;
pEvt->next = NULL;
// append to the event queue
// empty queue
if (!head && !tail) {
head = pEvt;
tail = pEvt;
}
else
tail->next = pEvt;
event_cnt++;
}
}
void EvtQueue::AddEvt(int evt) {
// search the event if exist
for (EvtEntry *p = head; p; p = p->next) {
if (p->event = evt)
break;
}
// no found
if (!p) {
EvtEntry *pEvt = new EvtEntry();
if (pEvt) {
// initialization
pEvt->event = evt;
pEvt->next = NULL;
// append to the event queue
// empty queue
if (!head && !tail) {
head = pEvt;
tail = pEvt;
}
else
tail->next = pEvt;
event_cnt++;
}
}
}
int EvtQueue::RemoveEvt() {
int evt = 0;
EvtEntry *pEvt = NULL;
// get an event
if (head){
pEvt = head;
head = head->next;
event_cnt--;
evt = pEvt->event;
delete pEvt;
}
return evt;
}
int EvtQueue::getEvtCnt() {
return event_cnt;
}
char* sig_msg(int evt) {
static char *msg[SIG_MAX] = {
"signal abort",
"signal stop",
"signal kill",
"signal child",
"unknown signal"
};
evt--;
if ((evt >= 0) && (evt < SIG_MAX))
return msg[evt];
else
return msg[SIG_MAX - 1];
}
// Global
int getRand(float m) {
int ret = 0;
srand((int)time(0));
while (true) {
ret = (int) (m * rand() / (RAND_MAX + 1.0));
if ((ret > 0) && (ret <=(int)m))
return ret;
}
}
//main
int main() {
EvtQueue evtQue;
for (int i = 0; i < SIG_MAX; i++) {
evtQue.AppendEvt(getRand(SIG_MAX));
}
for (int n = 0; n < SIG_MAX; n++) {
int evt = evtQue.RemoveEvt();
cout<<sig_msg(evt)<<endl;
}
return 0;
}
- 2009 March 12th Thursday (三月 十二日 木曜日)
- March 22th Thursday (三月 二十二日 木曜日)
- March 12th Monday (三月 十二日 月曜日)
- 2009 March 5th Thursday (三月 五日 木曜日)
- 2009 March 19th Thursday (三月 十九日 木曜日)
- 2009 March 26th Thursday (三月 二十六日 木曜日)
- March 8th Thursday (三月 八日 木曜日)
- March 15th Thursday (三月 十五日 木曜日)
- March 29th Thursday (三月 二十九日 木曜日)
- 2008 March 6th Thursday (三月 六日 木曜日
- 2008 March 14th Thursday (三月 十四日 木曜日)
- 2008 March 20th Thursday (三月 二十日 木曜日)
- 2008 March 27th Thursday (三月 二十七日 木曜日)
- February 12th Thursday 2009 (二月 十二日 木曜日)
- Aprial 12th Thursday (四月 十二日 木曜日)
- July 12th Thursday (七月 十二日 木曜日)
- January 22th Thursday 2009 (一月 二十二日 木曜日)
- March 1st Thursday (三月 一日 木曜日)
- 2009 March 6th Friday (三月 六日 金曜日)
- 2009 March 9th Monday (三月 九日 月曜日)
- iptrace占用/dev/bpf0导致tcpdump报 BIOCSETIF: en1: Do not specify an existing file
- 2009 March 10th Tuesday (三月 十日 火曜日)
- 2009 March 11th Wednesday (三月 十一日 水曜日)
- 2009 March 12th Thursday (三月 十二日 木曜日)
- 解除当前设置不允许下载该文件
- 2009 March 13th Friday (三月 十三日 金曜日)
- 2009 March 16th Monday (三月 十六日 月曜日)
- LuaForS60 工程进度 2.1 --- 3.30
- 2009 March 17th Tuesday (三月 十七日 火曜日)
- 2009 March 18th Wednesday (三月 十八日 水曜日)
- 使用c++/CLI实现c++托管与非托管混合编程
- 2009 March 19th Thursday (三月 十九日 木曜日)