当前位置:   article > 正文

用C语言实现一个线程池_c语言线程池的实现

c语言线程池的实现
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4. // 线程池中最大线程数
  5. #define MAX_THREADS 10
  6. // 任务结构体,包含任务执行函数和参数
  7. typedef struct {
  8. void *(*task)(void *);
  9. void *arg;
  10. } task_t;
  11. // 线程池结构体,包含互斥锁、条件变量、任务队列等
  12. typedef struct {
  13. pthread_mutex_t lock; // 线程池互斥锁
  14. pthread_cond_t notify; // 条件变量,用于通知空闲线程有新任务可做
  15. pthread_t threads[MAX_THREADS]; // 线程池中的线程
  16. task_t *tasks; // 任务队列
  17. int num_threads; // 当前线程池中的线程数
  18. int max_tasks; // 任务队列中最多允许的任务数
  19. int head; // 队列头指针
  20. int tail; // 队列尾指针
  21. int count; // 当前队列中的任务数量
  22. int shutdown; // 线程池是否关闭标志
  23. } thread_pool_t;
  24. // 初始化线程池
  25. int thread_pool_init(thread_pool_t *pool, int num_threads, int max_tasks) {
  26. pool->num_threads = 0;
  27. pool->max_tasks = max_tasks;
  28. pool->head = 0;
  29. pool->tail = 0;
  30. pool->count = 0;
  31. pool->shutdown = 0;
  32. // 初始化互斥锁和条件变量
  33. if (pthread_mutex_init(&(pool->lock), NULL) != 0 ||
  34. pthread_cond_init(&(pool->notify), NULL) != 0) {
  35. return -1;
  36. }
  37. // 创建指定数量的线程
  38. for (int i = 0; i < num_threads; i++) {
  39. if (pthread_create(&(pool->threads[i]), NULL, thread_pool_worker, (void *)pool) != 0) {
  40. return -1;
  41. }
  42. pool->num_threads++;
  43. }
  44. // 创建任务队列
  45. pool->tasks = (task_t *)malloc(max_tasks * sizeof(task_t));
  46. if (pool->tasks == NULL) {
  47. return -1;
  48. }
  49. return 0;
  50. }
  51. // 向线程池中添加新任务
  52. int thread_pool_add_task(thread_pool_t *pool, void *(*task)(void *), void *arg) {
  53. // 加锁,保证线程安全
  54. pthread_mutex_lock(&(pool->lock));
  55. // 检查任务队列是否已满
  56. if (pool->count == pool->max_tasks || pool->shutdown) {
  57. pthread_mutex_unlock(&(pool->lock));
  58. return -1;
  59. }
  60. // 将任务加入队列并更新计数器
  61. task_t new_task = {task, arg};
  62. pool->tasks[pool->tail] = new_task;
  63. pool->tail = (pool->tail + 1) % pool->max_tasks;
  64. pool->count++;
  65. // 通知空闲线程有新任务可做
  66. pthread_cond_signal(&(pool->notify));
  67. // 解锁
  68. pthread_mutex_unlock(&(pool->lock));
  69. return 0;
  70. }
  71. // 销毁线程池
  72. int thread_pool_destroy(thread_pool_t *pool) {
  73. pool->shutdown = 1;
  74. // 等待所有任务执行完毕
  75. while (pool->count > 0) {
  76. sleep(1);
  77. }
  78. // 终止所有线程并释放资源
  79. for (int i = 0; i < pool->num_threads; i++) {
  80. pthread_cancel(pool->threads[i]);
  81. pthread_join(pool->threads[i], NULL);
  82. }
  83. free(pool->tasks);
  84. pthread_mutex_destroy(&(pool->lock));
  85. pthread_cond_destroy(&(pool->notify));
  86. return 0;
  87. }
  88. // 线程池的工作线程,从任务队列中取出一个任务并执行
  89. void *thread_pool_worker(void *arg) {
  90. thread_pool_t *pool = (thread_pool_t *)arg;
  91. while (1) {
  92. // 加锁,保证线程安全
  93. pthread_mutex_lock(&(pool->lock));
  94. // 如果任务队列为空,则等待通知有新任务可做
  95. while (pool->count == 0 && !pool->shutdown) {
  96. pthread_cond_wait(&(pool->notify), &(pool->lock));
  97. }
  98. // 如果线程池已关闭,则退出循环结束线程
  99. if (pool->shutdown) {
  100. pthread_mutex_unlock(&(pool->lock));
  101. pthread_exit(NULL);
  102. }
  103. // 取出一个任务并更新计数器
  104. task_t task = pool->tasks[pool->head];
  105. pool->head = (pool->head + 1) % pool->max_tasks;
  106. pool->count--;
  107. // 解锁
  108. pthread_mutex_unlock(&(pool->lock));
  109. // 执行任务
  110. (*(task.task))(task.arg);
  111. }
  112. return NULL;
  113. }
  114. // 示例任务执行函数
  115. void *example_task(void *arg) {
  116. int num = *((int *)arg);
  117. printf("Task #%d started.\n", num);
  118. sleep(1); // 模拟任务执行过程
  119. printf("Task #%d ended.\n", num);
  120. return NULL;
  121. }
  122. // 测试代码
  123. int main() {
  124. thread_pool_t pool;
  125. // 初始化线程池
  126. if (thread_pool_init(&pool, MAX_THREADS, 10) != 0) {
  127. printf("Error initializing thread pool.\n");
  128. return -1;
  129. }
  130. // 向线程池中添加多个任务
  131. for (int i = 0; i < 15; i++) {
  132. int *num = (int *)malloc(sizeof(int));
  133. *num = i;
  134. thread_pool_add_task(&pool, example_task, num);
  135. }
  136. // 等待任务执行完毕并销毁线程池
  137. thread_pool_destroy(&pool);
  138. return 0;
  139. }

上述代码中,先定义了一个任务结构体和一个线程池结构体,分别用于存储任务的执行函数和参数,以及线程池中的相关信息。在初始化线程池时,会创建指定数量的线程,并将其加入到线程池中,并创建一个任务队列。添加任务时,需要使用加锁操作保证多线程安全,并将任务加入到队列中,并唤醒空闲线程开始执行任务。每个工作线程从任务队列中取出一个任务并执行,如果队列为空则等待通知。在销毁线程池时,会等待所有任务执行完毕后终止所有线程并释放占用的资源。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/621831
推荐阅读
相关标签
  

闽ICP备14008679号