データ構造とアルゴリズムの20200119のタスクキュースレッドプール

タスクキューは、関連する機能は、対応する関数、下記組成の構造によって行われます。

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

チェーン比較的共通キュー、機能ハンドラへのポインタへのデータ型は、
タスクIDを向上させながら、他の人が共通キューチェーンを継承しています。コンセプトは、スレッドプールは既製、既製1がフリーであるとき、彼は適切な治療を行い、タスクチームチームが一覧表示されますのセットで構成され、スレッドプールに関するものです。
ここに画像を挿入説明次のようにコードの残りの部分はあります

#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;
}
公開された53元の記事 ウォンの賞賛0 ビュー2107

おすすめ

転載: blog.csdn.net/weixin_40071289/article/details/104038478