線程池-由一個任務隊列和一組處理隊列的線程組成。一旦工作進程需要處理某個可能“阻塞”的操作,不用自己操作,將其作為一個任務放到線程池的隊列,接着會被某個空閑線程提取處理。
1 #include <stdio.h> 2 #include <assert.h> 3 #include <Windows.h> 4 #include <iostream> 5 #include <iomanip> 6 7 using namespace std; 8 9 #define MaxSize 1000 //隊列的最大容量 10 11 typedef struct _QNode //任務結點結構 12 { 13 int id; 14 void(*handler)(void); 15 struct _QNode* next; 16 }QNode; 17 18 typedef QNode* QueuePtr; 19 20 typedef struct Queue 21 { 22 int length; //隊列的長度 23 QueuePtr front; //隊頭指針 24 QueuePtr rear; //隊尾指針 25 }LinkQueue; 26 27 //分配線程執行的任務節點 28 QueuePtr thread_task_alloc() 29 { 30 QNode* task; 31 task = (QNode*)calloc(1, sizeof(QNode)); 32 if (task == NULL) 33 { 34 return NULL; 35 } 36 return task; 37 } 38 39 //隊列初始化,將隊列初始化為空隊列 40 void InitQueue(LinkQueue* LQ) 41 { 42 if (!LQ) return; 43 LQ->length = 0; 44 LQ->front = LQ->rear = NULL; //把對頭和隊尾指針同時置0 45 } 46 47 //判斷隊列為空 48 int IsEmpty(LinkQueue* LQ) 49 { 50 if (!LQ) return 0; 51 if (LQ->front == NULL) 52 { 53 return 1; 54 } return 0; 55 } 56 57 //判斷隊列是否為滿 58 int IsFull(LinkQueue* LQ) 59 { 60 if (!LQ) return 0; 61 if (LQ->length == MaxSize) 62 { 63 return 1; 64 } return 0; 65 } 66 67 //入隊,將元素data插入到隊列LQ中 68 int EnterQueue(LinkQueue* LQ, QNode* node) 69 { 70 if (!LQ || !node) return 0; 71 if (IsFull(LQ)) 72 { 73 cout << "無法插入任務 " << node->id << ", 隊列已滿!" << endl; return 0; 74 } node->next = NULL; 75 if (IsEmpty(LQ)) //空隊列 76 { 77 LQ->front = LQ->rear = node; 78 } 79 else 80 { 81 LQ->rear->next = node; //在隊尾插入節點qNode 82 LQ->rear = node; //隊尾指向新插入的節點 83 } 84 LQ->length++; 85 return 1; 86 } 87 88 //出隊,將隊列中隊頭的節點出隊,返回頭節點 89 QNode* PopQueue(LinkQueue* LQ) 90 { 91 QNode* tmp = NULL; 92 if (!LQ || IsEmpty(LQ)) 93 { 94 cout << "隊列為空!" << endl; 95 return 0; 96 } tmp = LQ->front; 97 98 LQ->front = tmp->next; 99 100 if (!LQ->front) 101 { 102 LQ->rear = NULL;//如果對頭出列后不存在其他元素,則rear節點也要置空 103 LQ->length--; 104 } 105 return tmp; 106 } 107 108 //打印隊列中的各元素 109 void PrintQueue(LinkQueue* LQ) 110 { 111 QueuePtr tmp; 112 if (!LQ) return; 113 if (LQ->front == NULL) 114 { 115 cout << "隊列為空!"; return; 116 } 117 tmp = LQ->front; 118 while (tmp) 119 { 120 cout << setw(4) << tmp->id; 121 tmp = tmp->next; 122 } 123 cout << endl; 124 } 125 126 //獲取隊列中元素的個數 127 int getLength(LinkQueue* LQ) 128 { 129 if (!LQ) return 0; 130 return LQ->length; 131 } 132 133 void task1() 134 { 135 printf("我是任務1 ...\n"); 136 } 137 void task2() 138 { 139 printf("我是任務2 ...\n"); 140 } 141 142 143 int main() 144 { 145 LinkQueue* LQ = new LinkQueue; 146 QNode* task = NULL; 147 //初始化隊列 148 InitQueue(LQ); 149 150 //任務1入隊 151 task = thread_task_alloc(); 152 task->id = 1; 153 task->handler = &task1; 154 EnterQueue(LQ, task); 155 156 //任務2入隊 157 task = thread_task_alloc(); 158 task->id = 2; 159 task->handler = &task2; 160 EnterQueue(LQ, task); 161 162 //打印任務隊列中的元素 163 printf("隊列中的元素(總共%d 個):", getLength(LQ)); 164 PrintQueue(LQ); 165 cout << endl; 166 167 //執行任務 168 while ((task = PopQueue(LQ))) 169 { 170 task->handler(); 171 delete task; 172 } 173 174 //清理資源 175 delete LQ; 176 system("pause"); 177 return 0; 178 }
=================================================================================================================