1、简单的队列入队、出队实现,
2、控制台线程开启定时器,定时入队
3、控制台获取系统时间,并随出队打印
queue.h文件
#ifndef QUEUE_H_
#define QUEUE_H_
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "time.h"
#define QUEUE_OK 1
#define QUEUE_ERR -1
#define QUEUE_TURE 1
#define QUEUE_FALSE 0
#define QUEUE_SIZE 20
#define NO_CASE 1 /*没有电量*/
#define ONE_CASE 2 /*一格电量*/
#define TWO_CASE 3 /*两格电量*/
#define THREE_CASE 4 /*三格电量*/
#define FOUR_CASE 5 /*四格电量 -- 满电量*/
typedef struct _QUEUE_ QUEUE_S;
struct _QUEUE_
{
unsigned char battery[QUEUE_SIZE];
int front;
int rear;
int(*Queue_Init)(QUEUE_S*);
int(*Queue_Insert)(QUEUE_S*,unsigned char );
int(*Queue_Delete)(QUEUE_S*,unsigned char*);
};
//判断队列是否满 -- 队头是否等于队尾+1
#define Is_Queue_Full(p) p->front == (p->rear+1)%QUEUE_SIZE ? QUEUE_TURE : QUEUE_FALSE
//判断队列是否为空 --- 队头是否等于队尾
#define Is_Queue_Empty(p) p->front == p->rear ? QUEUE_TURE : QUEUE_FALSE
extern QUEUE_S g_queue;
#endif
queue.c文件
#include "queue.h"
/*
队列只能在队尾插入 只能在对头删除
*/
int Queue_Init_t(QUEUE_S* pQueue)
{
if (pQueue == NULL)
{
return QUEUE_ERR;
}
pQueue->front = 0;
pQueue->rear = 0;
memset(pQueue->battery,0,QUEUE_SIZE);
return QUEUE_OK;
}
/*
函数功能:入队
参数1:队列指针
参数2:入队内容
*/
int Queue_Insert_t(QUEUE_S* pQueue,unsigned char power_grid)
{
if (pQueue == NULL)
{
return QUEUE_ERR;
}
if (Is_Queue_Full(pQueue) == QUEUE_TURE) //队列已经满了
{
printf("Queue is Full\r\n");
return QUEUE_ERR;
}
pQueue->battery[pQueue->rear] = power_grid;
pQueue->rear = (pQueue->rear + 1)%QUEUE_SIZE;
return QUEUE_OK;
}
/*
函数功能:出队
参数1:队列指针
参数2:保存出队内容的指针
*/
int Queue_Delete_t(QUEUE_S* pQueue,unsigned char *p_power_grid)
{
if (pQueue == NULL)
{
return QUEUE_ERR;
}
if (Is_Queue_Empty(pQueue) == QUEUE_TURE) // 队列为空
{
return QUEUE_ERR;
}
*p_power_grid = pQueue->battery[pQueue->front];
pQueue->front = (pQueue->front + 1)%QUEUE_SIZE;
return QUEUE_OK;
}
QUEUE_S g_queue =
{
{0},
0,
0,
Queue_Init_t,
Queue_Insert_t,
Queue_Delete_t,
};
main.c文件
#include "queue.h"
#include <stdio.h>
#include <windows.h>
#include <conio.h>
UINT cnt = 0;
//定时器回调函数
void CALLBACK TimeProc(HWND hwnd, UINT message, UINT idTimer, DWORD dwTime);
//线程回调函数
DWORD CALLBACK ThreadProc(PVOID pvoid);
//主函数
int main()
{
unsigned char k;
SYSTEMTIME st;
//创建线程
DWORD dwThreadId;
HANDLE hThread;
g_queue.Queue_Init(&g_queue); //先初始化队列
hThread = CreateThread(NULL, 0, ThreadProc, 0, 0, &dwThreadId); //开启线程
printf("hello, thread start!\n");
while(1)
{
if (g_queue.Queue_Delete(&g_queue,&k) == QUEUE_OK)
{
GetLocalTime(&st);
printf("%04d-%02d-%02d %02d:%02d:%02d:%03d\t",
st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
printf("出队内容:%d\r\n",k);
}
}
//得到键盘输入后再退出
system("pause");
return 0;
}
//线程
DWORD CALLBACK ThreadProc(PVOID pvoid)
{
//强制系统为线程简历消息队列
MSG msg;
PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
//设置定时器
SetTimer(NULL, 10, 1000, TimeProc);
//获取并分发消息
while(GetMessage(&msg, NULL, 0, 0))
{
if(msg.message == WM_TIMER)
{
TranslateMessage(&msg); // 翻译消息
DispatchMessage(&msg); // 分发消息
}
}
KillTimer(NULL, 10);
printf("thread end here\n");
return 0;
}
//定时事件
void CALLBACK TimeProc(HWND hwnd, UINT message, UINT idTimer, DWORD dwTime)
{
cnt = cnt >= 255 ? 0 : cnt+1;
g_queue.Queue_Insert(&g_queue,cnt);
//printf("thread count = %d\n", cnt);
}