源码:#include<stdio.h>#include<ctype.h>#include<unistd.h>#include<stdlib.h>#include<sys/types.h>#include<sys/stat.h>#include<sys/select.h>#include<sys/epoll.h>#include<string.h>#include<arpa/inet.h>#include<pthread.h>#include<fcntl.h>#include<errno.h>pthread_mutex_t mutexpool; int tasks_quantity = 0;typedef struct _Taskpool{void (*function)(void* arg);//void* arg; //struct _Taskpool* next;//子节点}Taskpool, * pTaskpool;pTaskpool head =NULL, tail;void* add_task(void*arg)//添加任务{pTaskpool task = (pTaskpool)malloc(sizeof(Taskpool));if (task == NULL){perror("task malloc 失败\n");exit(1);}task->next == NULL;pthread_mutex_lock(&mutexpool);if (head == NULL){head = task;tail = task;}else{tail->next = task;}tasks_quantity += 1;printf("head = %ld,tasks_quantity = %d\n", head, tasks_quantity);pthread_mutex_unlock(&mutexpool);}void* add(void* arg){while(1){add_task(NULL);usleep(20000);}}void* work(void* arg){pTaskpool ptaskpool= NULL;int sw = 0;while (1){pthread_mutex_lock(&mutexpool);if (tasks_quantity > 0){if (head->next == NULL){free(head);head = NULL;}else{ptaskpool = head;head = head->next;free(ptaskpool);}tasks_quantity -= 1;}pthread_mutex_unlock(&mutexpool);}}int main(){pthread_t tid;pthread_create(&tid,NULL, work,NULL);int x = 0;while (x<5){x++;pthread_t tid2;pthread_create(&tid2, NULL, add, NULL);}pthread_exit(NULL);return 0;}