Task queue thread pool of 20,200,119 of data structures and algorithms

Task queue, the associated function is performed by the corresponding function, the structure of the following composition:

typedef struct _linlList {
	int id;
	void (*handler)();//函数指针
	_linlList* next;
}linkList,linkNode;
typedef struct _linkQueue {
	int length;
	linkList* front;
	linkList* rear;
}linkQueueNode, linkQueue;

Chain relatively common queue, the data type into pointers to functions Handler,
while increasing the task id, others have inherited common queue chain. The concept also relates to the thread pool, thread pool is composed of a set of ready-made, ready-made when one is free, he will list the task team team, make the appropriate treatment.
Here Insert Picture DescriptionThe rest of the code is as follows

#include<Windows.h>
#include<iostream>
#define MAX_SIZE 10
using namespace std;
typedef int datatype;
typedef struct _linlList {
	int id;
	void (*handler)();//函数指针
	_linlList* next;
}linkList,linkNode;
typedef struct _linkQueue {
	int length;
	linkList* front;
	linkList* rear;
}linkQueueNode, linkQueue;
linkNode* thread_task() {
	linkNode* task;
	task = new linkNode;
	if (!task) {
		return NULL;
	}
	return task;
};
void task_01(){
	cout << "马上过年了!" << endl;
}
void task_02() {
	cout << "让我去办年货!" << endl;
}

bool initLinkQueue(linkQueue*& lq);//链式队列初始化
bool linkQueueEnter(linkQueue*& lq, linkNode* & node);//入队
void linkQueuePrint(linkQueue*& lq);
linkNode* linkQueueDelete(linkQueue*& lq);//出队
bool linkQueueGetHead(linkQueue*& lq, datatype* data);//获取队首的元素,不出队
void linkQueueClear(linkQueue*& lq);//清空队列
int linkQueueGetLength(linkQueue*& lq);//获取队列的长度
void linkQueueDestroy(linkQueue*& lq);//销毁列表

static int st_id = 0;
int main() {
	linkQueue* lq;
	initLinkQueue(lq);

	//--入队
	linkNode* node = NULL;
	node = new linkNode;
	node->id = ++st_id;
	node->handler = &task_01;
	linkQueueEnter(lq, node);

	node = new linkNode;
	node->id = ++st_id;
	node->handler = &task_02;
	linkQueueEnter(lq, node);
	//打印任务队列中的元素
	cout << "队列中共有的元素个数为" << linkQueueGetLength(lq)<<endl;
	linkQueuePrint(lq);

	//任务队列运行
	while (1) {
		node = linkQueueDelete(lq);
		if (!node) break;
		node->handler();
		delete node;
	}

	linkQueueDestroy(lq);
	system("pause");
	return 0;
}
bool initLinkQueue(linkQueue*& lq) {
	lq = new linkQueue;
	if (!lq) return false;
	lq->length = 0;
	lq->front = lq->rear = NULL;
	return true;
}
bool linkQueueEnter(linkQueue*& lq, linkNode* &node) {
	if (!lq || !node|| lq->length == MAX_SIZE) return false;
	
	node->next = NULL;
	if (!lq->front) {
		lq->rear = lq->front = node;
	}
	else {
		lq->rear->next = node;
		lq->rear = node;
	}
	lq->length++;
	return true;
}
void linkQueuePrint(linkQueue*& lq) {
	if (!lq) return;
	if (!lq->length) {
		cout << "队列长度为0" << endl;
		return;
	}
	linkList* tmp = lq->front;
	while (tmp) {
		cout << tmp->id << " ";
		tmp = tmp->next;
	}
	cout << endl;
}
linkNode* linkQueueDelete(linkQueue*& lq) {
	if (!lq || !lq->length) return NULL;
	linkNode* node=new linkNode;
	node= lq->front;	
	lq->length--;
	lq->front = node->next;
	if (!lq->front) lq->rear = NULL;
	return node;
}
bool linkQueueGetHead(linkQueue*& lq, datatype* data) {
	if (!lq || !lq->length) return false;
	*data = lq->front->id;
	return true;
}
int linkQueueGetLength(linkQueue*& lq) {
	if (!lq) return -1;
	return lq->length;
}
void linkQueueClear(linkQueue*& lq) {
	if (!lq || !lq->length) return;
	linkList* tmp = NULL;
	while (lq->front) {
		tmp = lq->front->next;
		delete lq->front;
		lq->front = tmp;
	}
	lq->front = lq->rear = NULL;
	lq->length = 0;
}
void linkQueueDestroy(linkQueue*& lq) {
	if (!lq || !lq->length) return;
	linkList* tmp = lq->front;
	while (lq->front) {
		tmp = lq->front->next;
		delete lq->front;
		lq->front = tmp;
	}
	if(lq->front )delete lq->front;
	//if(lq->rear) delete lq->rear;
	delete lq;
}
Published 53 original articles · won praise 0 · Views 2107

Guess you like

Origin blog.csdn.net/weixin_40071289/article/details/104038478