线程池的设计与测试

编写了一个最基本的线程池类,处理用c_work表示的工作任务。C++还很不熟练,欢迎会C++的提出宝贵的修改意见。

程序有注释,所以应该很好读懂。测试程序在下面。




  1. ///  
  2. //线程池类   
  3. ///  
  4. #include <pthread.h>  
  5. #include <stdlib.h>  
  6. #include <stdio.h>  
  7. #include <unistd.h>  
  8. #include <assert.h>  
  9.   
  10. const int DEFAULT_MAX_THREAD_NUM = 10;  
  11. const int MAX_WORK_NUM = 100000;  
  12. //c_worker类  
  13. class c_work  
  14. {  
  15.     public:  
  16.         c_work():process(NULL), arg(NULL), next(NULL){}  
  17.         c_work(void *(*prcss)(void *), void *arg):  
  18.             process(prcss), arg(arg), next(NULL) {}  
  19.         ~c_work();   
  20.   
  21.         void *(*process)(void *);  
  22.         void *arg;  
  23.         unsigned char type; //最高位表示arg是否需要delete操作  
  24.         c_work *next;  
  25. };  
  26.   
  27. c_work::~c_work()  
  28. {  
  29.     unsigned char ifdel = type >> 7;  
  30.     if (ifdel)  
  31.     {  
  32.         delete arg;  
  33.         arg = NULL;  
  34.     }  
  35. }  
  36.   
  37. class c_thread_pool  
  38. {  
  39.     public:  
  40.         c_thread_pool();  
  41.         c_thread_pool(const int max_thread_num);  
  42.         ~c_thread_pool();  
  43.   
  44.         int add_work(c_work work);  
  45.         static void *thread_routine(void *arg);  
  46.   
  47.         pthread_mutex_t queue_lock;  
  48.         pthread_cond_t queue_cond;  
  49.   
  50. //  private:  
  51.         c_work *queue_head;  
  52.         c_work *queue_tail;  
  53.         int shutdown;  
  54.   
  55.         pthread_t *threadid;  
  56.         int max_thread_num;  
  57.         int cur_queue_size;  
  58. };  
  59.   
  60. c_thread_pool::c_thread_pool()  
  61. {  
  62.   
  63.     pthread_mutex_init(&queue_lock, NULL);  
  64.     pthread_cond_init(&queue_cond, NULL);  
  65.   
  66.     //工作队列初始化  
  67.     queue_head = NULL;  
  68.     queue_tail = NULL;  
  69.       
  70.     max_thread_num = max_thread_num;  
  71.     cur_queue_size = 0;  
  72.   
  73.     shutdown = 0;  
  74.   
  75.     max_thread_num = DEFAULT_MAX_THREAD_NUM;  
  76.     threadid = new pthread_t[max_thread_num];  
  77.     int i = 0;  
  78.   
  79.     for (i = 0; i < max_thread_num; i++)  
  80.     {  
  81.         pthread_create(&(threadid[i]), NULL, thread_routine, (void*)this);  
  82.     }  
  83. }  
  84.   
  85.   
  86. c_thread_pool::c_thread_pool(int max_thread_num)  
  87. {  
  88.   
  89.     pthread_mutex_init(&queue_lock, NULL);  
  90.     pthread_cond_init(&queue_cond, NULL);  
  91.   
  92.     //工作队列初始化  
  93.     queue_head = NULL;  
  94.     queue_tail = NULL;  
  95.       
  96.     max_thread_num = max_thread_num;  
  97.     cur_queue_size = 0;  
  98.   
  99.     threadid = new pthread_t[max_thread_num];  
  100.     int i = 0;  
  101.     for (i = 0; i < max_thread_num; i++)  
  102.     {  
  103.         pthread_create(&(threadid[i]), NULL, thread_routine, (void*)this);  
  104.     }  
  105. }  
  106.   
  107. /*向线程池中的任务队列加入任务*/  
  108. int c_thread_pool::add_work(c_work work)  
  109. {  
  110.     c_work *newwork = new c_work;  
  111.     newwork->process = work.process;  
  112.     newwork->arg = work.arg;  
  113.     newwork->next = NULL;  
  114.   
  115.     pthread_mutex_lock(&queue_lock);  
  116.       
  117.     /*将任务加入到等待队列中*/  
  118.     if (queue_head != NULL && queue_tail != NULL)  
  119.     {  
  120.         queue_tail->next = newwork;  
  121.         queue_tail = newwork;     
  122.     }  
  123.     else  
  124.     {  
  125.         //空队列  
  126.         queue_head = newwork;  
  127.         queue_tail = newwork;  
  128.     }  
  129.   
  130.     cur_queue_size++;  
  131.     pthread_mutex_unlock(&queue_lock);  
  132.     /*等待队列中有任务了,唤醒一个等待线程,注意如果所有线程都在忙碌,这句没有任何作用*/  
  133.     pthread_cond_signal(&(queue_cond));   
  134.     printf("add work returned!\n");  
  135.     return 0;  
  136. }  
  137.   
  138.   
  139. void* c_thread_pool::thread_routine(void *arg)  
  140. {  
  141.     c_thread_pool *pool = (c_thread_pool *)arg;  
  142.     int i = 0;  
  143.     while (1)  
  144.     {  
  145.         pthread_mutex_lock(&(pool->queue_lock));  
  146.         //如果等待队列为0并且不销毁线程池,则处于阻塞状态; 注意  
  147.          // pthread_cond_wait是一个原子操作,等待前会解锁,唤醒后会加锁  
  148.   
  149.         //标注:注意这一如果任务队列不为空的话,while语句将被跳过,直接执行下面的调用。  
  150.         while (pool->cur_queue_size == 0 && pool->shutdown)  
  151.         {  
  152.             pthread_cond_wait(&(pool->queue_cond), &(pool->queue_lock));  
  153.         }  
  154.   
  155.   
  156.         //等待队列长度减去1,并取出链表中的头元素  
  157.         if (pool->cur_queue_size > 0 && pool->queue_head != NULL)  
  158.         {  
  159.             printf("IN THREAD ROUTINE size = %d && queue head is not NULL\n", pool->cur_queue_size);  
  160.             pool->cur_queue_size--;  
  161.             c_work *work = pool->queue_head;  
  162.             pool->queue_head = work->next;  
  163.             pthread_mutex_unlock(&(pool->queue_lock));  
  164.   
  165.             //调用回调函数,执行测试任务  
  166.             //  
  167.             (*(work->process))(work->arg);  
  168.             free(work);  
  169.             work = NULL;  
  170.         }  
  171.         else //不可达  
  172.         {  
  173.             pthread_mutex_unlock(&(pool->queue_lock));  
  174.         }  
  175.     }  
  176.       
  177. }  
  178.   
  179. c_thread_pool::~c_thread_pool()  
  180. {  
  181.     for (int i = 0; i < max_thread_num; ++i)   
  182.         pthread_cancel(threadid[i]);  
  183.     for (c_work *w_t = queue_head; w_t != NULL;)  
  184.     {  
  185.         c_work *temp = w_t->next;  
  186.         delete w_t;  
  187.         w_t = temp;  
  188.     }  
  189.     delete [] threadid;  
  190. }  



  1. ///  
  2. //线程池类   
  3. ///  
  4. #include <pthread.h>  
  5. #include <stdlib.h>  
  6. #include <stdio.h>  
  7. #include <unistd.h>  
  8. #include <assert.h>  
  9.   
  10. const int DEFAULT_MAX_THREAD_NUM = 10;  
  11. const int MAX_WORK_NUM = 100000;  
  12. //c_worker类  
  13. class c_work  
  14. {  
  15.     public:  
  16.         c_work():process(NULL), arg(NULL), next(NULL){}  
  17.         c_work(void *(*prcss)(void *), void *arg):  
  18.             process(prcss), arg(arg), next(NULL) {}  
  19.         ~c_work();   
  20.   
  21.         void *(*process)(void *);  
  22.         void *arg;  
  23.         unsigned char type; //最高位表示arg是否需要delete操作  
  24.         c_work *next;  
  25. };  
  26.   
  27. c_work::~c_work()  
  28. {  
  29.     unsigned char ifdel = type >> 7;  
  30.     if (ifdel)  
  31.     {  
  32.         delete arg;  
  33.         arg = NULL;  
  34.     }  
  35. }  
  36.   
  37. class c_thread_pool  
  38. {  
  39.     public:  
  40.         c_thread_pool();  
  41.         c_thread_pool(const int max_thread_num);  
  42.         ~c_thread_pool();  
  43.   
  44.         int add_work(c_work work);  
  45.         static void *thread_routine(void *arg);  
  46.   
  47.         pthread_mutex_t queue_lock;  
  48.         pthread_cond_t queue_cond;  
  49.   
  50. //  private:  
  51.         c_work *queue_head;  
  52.         c_work *queue_tail;  
  53.         int shutdown;  
  54.   
  55.         pthread_t *threadid;  
  56.         int max_thread_num;  
  57.         int cur_queue_size;  
  58. };  
  59.   
  60. c_thread_pool::c_thread_pool()  
  61. {  
  62.   
  63.     pthread_mutex_init(&queue_lock, NULL);  
  64.     pthread_cond_init(&queue_cond, NULL);  
  65.   
  66.     //工作队列初始化  
  67.     queue_head = NULL;  
  68.     queue_tail = NULL;  
  69.       
  70.     max_thread_num = max_thread_num;  
  71.     cur_queue_size = 0;  
  72.   
  73.     shutdown = 0;  
  74.   
  75.     max_thread_num = DEFAULT_MAX_THREAD_NUM;  
  76.     threadid = new pthread_t[max_thread_num];  
  77.     int i = 0;  
  78.   
  79.     for (i = 0; i < max_thread_num; i++)  
  80.     {  
  81.         pthread_create(&(threadid[i]), NULL, thread_routine, (void*)this);  
  82.     }  
  83. }  
  84.   
  85.   
  86. c_thread_pool::c_thread_pool(int max_thread_num)  
  87. {  
  88.   
  89.     pthread_mutex_init(&queue_lock, NULL);  
  90.     pthread_cond_init(&queue_cond, NULL);  
  91.   
  92.     //工作队列初始化  
  93.     queue_head = NULL;  
  94.     queue_tail = NULL;  
  95.       
  96.     max_thread_num = max_thread_num;  
  97.     cur_queue_size = 0;  
  98.   
  99.     threadid = new pthread_t[max_thread_num];  
  100.     int i = 0;  
  101.     for (i = 0; i < max_thread_num; i++)  
  102.     {  
  103.         pthread_create(&(threadid[i]), NULL, thread_routine, (void*)this);  
  104.     }  
  105. }  
  106.   
  107. /*向线程池中的任务队列加入任务*/  
  108. int c_thread_pool::add_work(c_work work)  
  109. {  
  110.     c_work *newwork = new c_work;  
  111.     newwork->process = work.process;  
  112.     newwork->arg = work.arg;  
  113.     newwork->next = NULL;  
  114.   
  115.     pthread_mutex_lock(&queue_lock);  
  116.       
  117.     /*将任务加入到等待队列中*/  
  118.     if (queue_head != NULL && queue_tail != NULL)  
  119.     {  
  120.         queue_tail->next = newwork;  
  121.         queue_tail = newwork;     
  122.     }  
  123.     else  
  124.     {  
  125.         //空队列  
  126.         queue_head = newwork;  
  127.         queue_tail = newwork;  
  128.     }  
  129.   
  130.     cur_queue_size++;  
  131.     pthread_mutex_unlock(&queue_lock);  
  132.     /*等待队列中有任务了,唤醒一个等待线程,注意如果所有线程都在忙碌,这句没有任何作用*/  
  133.     pthread_cond_signal(&(queue_cond));   
  134.     printf("add work returned!\n");  
  135.     return 0;  
  136. }  
  137.   
  138.   
  139. void* c_thread_pool::thread_routine(void *arg)  
  140. {  
  141.     c_thread_pool *pool = (c_thread_pool *)arg;  
  142.     int i = 0;  
  143.     while (1)  
  144.     {  
  145.         pthread_mutex_lock(&(pool->queue_lock));  
  146.         //如果等待队列为0并且不销毁线程池,则处于阻塞状态; 注意  
  147.          // pthread_cond_wait是一个原子操作,等待前会解锁,唤醒后会加锁  
  148.   
  149.         //标注:注意这一如果任务队列不为空的话,while语句将被跳过,直接执行下面的调用。  
  150.         while (pool->cur_queue_size == 0 && pool->shutdown)  
  151.         {  
  152.             pthread_cond_wait(&(pool->queue_cond), &(pool->queue_lock));  
  153.         }  
  154.   
  155.   
  156.         //等待队列长度减去1,并取出链表中的头元素  
  157.         if (pool->cur_queue_size > 0 && pool->queue_head != NULL)  
  158.         {  
  159.             printf("IN THREAD ROUTINE size = %d && queue head is not NULL\n", pool->cur_queue_size);  
  160.             pool->cur_queue_size--;  
  161.             c_work *work = pool->queue_head;  
  162.             pool->queue_head = work->next;  
  163.             pthread_mutex_unlock(&(pool->queue_lock));  
  164.   
  165.             //调用回调函数,执行测试任务  
  166.             //  
  167.             (*(work->process))(work->arg);  
  168.             free(work);  
  169.             work = NULL;  
  170.         }  
  171.         else //不可达  
  172.         {  
  173.             pthread_mutex_unlock(&(pool->queue_lock));  
  174.         }  
  175.     }  
  176.       
  177. }  
  178.   
  179. c_thread_pool::~c_thread_pool()  
  180. {  
  181.     for (int i = 0; i < max_thread_num; ++i)   
  182.         pthread_cancel(threadid[i]);  
  183.     for (c_work *w_t = queue_head; w_t != NULL;)  
  184.     {  
  185.         c_work *temp = w_t->next;  
  186.         delete w_t;  
  187.         w_t = temp;  
  188.     }  
  189.     delete [] threadid;  
  190. }  

  1. #include "___pool.h"  
  2.   
  3. struct adder  
  4. {  
  5.     int a;  
  6.     int b;  
  7. };  
  8.   
  9. void *process(void *add)  
  10. {  
  11.     struct adder *addp = (struct adder*)add;  
  12.     printf("-----in process:\n");  
  13.     printf("sum = %d + %d = %d\n", addp->a, addp->b, addp->a +  addp->b);  
  14.     return (void *)0;  
  15. }  
  16. int main(int argc, char **argv)  
  17. {  
  18.     if (argc < 2)  
  19.     {     
  20.         printf("input task num!\n");  
  21.         return -1;  
  22.     }  
  23.     int num = atoi(argv[1]);  
  24.     int i;  
  25.     c_thread_pool pool(10);           
  26.     struct adder add[num];  
  27.     for (i = 0; i < num; i++)  
  28.     {  
  29.         add[i].a = i + 1;  
  30.         add[i].b = i + 1;  
  31.     }  
  32.     c_worker wker[num];  
  33.     for (i = 0; i < num; i++)  
  34.     {  
  35.         wker[i].process = process;  
  36.         wker[i].arg = &add[i];  
  37.     }  
  38.     for (i = 0; i < num; i++)  
  39.     {  
  40.         pool.add_worker(wker[i]);  
  41.     }  
  42.     sleep(100);  
  43.     printf("\n");  
  44.     return 0;  
  45. }  


原文地址:http://blog.csdn.net/naturebe/article/details/7901130

  1. ///  
  2. //线程池类   
  3. ///  
  4. #include <pthread.h>  
  5. #include <stdlib.h>  
  6. #include <stdio.h>  
  7. #include <unistd.h>  
  8. #include <assert.h>  
  9.   
  10. const int DEFAULT_MAX_THREAD_NUM = 10;  
  11. const int MAX_WORK_NUM = 100000;  
  12. //c_worker类  
  13. class c_work  
  14. {  
  15.     public:  
  16.         c_work():process(NULL), arg(NULL), next(NULL){}  
  17.         c_work(void *(*prcss)(void *), void *arg):  
  18.             process(prcss), arg(arg), next(NULL) {}  
  19.         ~c_work();   
  20.   
  21.         void *(*process)(void *);  
  22.         void *arg;  
  23.         unsigned char type; //最高位表示arg是否需要delete操作  
  24.         c_work *next;  
  25. };  
  26.   
  27. c_work::~c_work()  
  28. {  
  29.     unsigned char ifdel = type >> 7;  
  30.     if (ifdel)  
  31.     {  
  32.         delete arg;  
  33.         arg = NULL;  
  34.     }  
  35. }  
  36.   
  37. class c_thread_pool  
  38. {  
  39.     public:  
  40.         c_thread_pool();  
  41.         c_thread_pool(const int max_thread_num);  
  42.         ~c_thread_pool();  
  43.   
  44.         int add_work(c_work work);  
  45.         static void *thread_routine(void *arg);  
  46.   
  47.         pthread_mutex_t queue_lock;  
  48.         pthread_cond_t queue_cond;  
  49.   
  50. //  private:  
  51.         c_work *queue_head;  
  52.         c_work *queue_tail;  
  53.         int shutdown;  
  54.   
  55.         pthread_t *threadid;  
  56.         int max_thread_num;  
  57.         int cur_queue_size;  
  58. };  
  59.   
  60. c_thread_pool::c_thread_pool()  
  61. {  
  62.   
  63.     pthread_mutex_init(&queue_lock, NULL);  
  64.     pthread_cond_init(&queue_cond, NULL);  
  65.   
  66.     //工作队列初始化  
  67.     queue_head = NULL;  
  68.     queue_tail = NULL;  
  69.       
  70.     max_thread_num = max_thread_num;  
  71.     cur_queue_size = 0;  
  72.   
  73.     shutdown = 0;  
  74.   
  75.     max_thread_num = DEFAULT_MAX_THREAD_NUM;  
  76.     threadid = new pthread_t[max_thread_num];  
  77.     int i = 0;  
  78.   
  79.     for (i = 0; i < max_thread_num; i++)  
  80.     {  
  81.         pthread_create(&(threadid[i]), NULL, thread_routine, (void*)this);  
  82.     }  
  83. }  
  84.   
  85.   
  86. c_thread_pool::c_thread_pool(int max_thread_num)  
  87. {  
  88.   
  89.     pthread_mutex_init(&queue_lock, NULL);  
  90.     pthread_cond_init(&queue_cond, NULL);  
  91.   
  92.     //工作队列初始化  
  93.     queue_head = NULL;  
  94.     queue_tail = NULL;  
  95.       
  96.     max_thread_num = max_thread_num;  
  97.     cur_queue_size = 0;  
  98.   
  99.     threadid = new pthread_t[max_thread_num];  
  100.     int i = 0;  
  101.     for (i = 0; i < max_thread_num; i++)  
  102.     {  
  103.         pthread_create(&(threadid[i]), NULL, thread_routine, (void*)this);  
  104.     }  
  105. }  
  106.   
  107. /*向线程池中的任务队列加入任务*/  
  108. int c_thread_pool::add_work(c_work work)  
  109. {  
  110.     c_work *newwork = new c_work;  
  111.     newwork->process = work.process;  
  112.     newwork->arg = work.arg;  
  113.     newwork->next = NULL;  
  114.   
  115.     pthread_mutex_lock(&queue_lock);  
  116.       
  117.     /*将任务加入到等待队列中*/  
  118.     if (queue_head != NULL && queue_tail != NULL)  
  119.     {  
  120.         queue_tail->next = newwork;  
  121.         queue_tail = newwork;     
  122.     }  
  123.     else  
  124.     {  
  125.         //空队列  
  126.         queue_head = newwork;  
  127.         queue_tail = newwork;  
  128.     }  
  129.   
  130.     cur_queue_size++;  
  131.     pthread_mutex_unlock(&queue_lock);  
  132.     /*等待队列中有任务了,唤醒一个等待线程,注意如果所有线程都在忙碌,这句没有任何作用*/  
  133.     pthread_cond_signal(&(queue_cond));   
  134.     printf("add work returned!\n");  
  135.     return 0;  
  136. }  
  137.   
  138.   
  139. void* c_thread_pool::thread_routine(void *arg)  
  140. {  
  141.     c_thread_pool *pool = (c_thread_pool *)arg;  
  142.     int i = 0;  
  143.     while (1)  
  144.     {  
  145.         pthread_mutex_lock(&(pool->queue_lock));  
  146.         //如果等待队列为0并且不销毁线程池,则处于阻塞状态; 注意  
  147.          // pthread_cond_wait是一个原子操作,等待前会解锁,唤醒后会加锁  
  148.   
  149.         //标注:注意这一如果任务队列不为空的话,while语句将被跳过,直接执行下面的调用。  
  150.         while (pool->cur_queue_size == 0 && pool->shutdown)  
  151.         {  
  152.             pthread_cond_wait(&(pool->queue_cond), &(pool->queue_lock));  
  153.         }  
  154.   
  155.   
  156.         //等待队列长度减去1,并取出链表中的头元素  
  157.         if (pool->cur_queue_size > 0 && pool->queue_head != NULL)  
  158.         {  
  159.             printf("IN THREAD ROUTINE size = %d && queue head is not NULL\n", pool->cur_queue_size);  
  160.             pool->cur_queue_size--;  
  161.             c_work *work = pool->queue_head;  
  162.             pool->queue_head = work->next;  
  163.             pthread_mutex_unlock(&(pool->queue_lock));  
  164.   
  165.             //调用回调函数,执行测试任务  
  166.             //  
  167.             (*(work->process))(work->arg);  
  168.             free(work);  
  169.             work = NULL;  
  170.         }  
  171.         else //不可达  
  172.         {  
  173.             pthread_mutex_unlock(&(pool->queue_lock));  
  174.         }  
  175.     }  
  176.       
  177. }  
  178.   
  179. c_thread_pool::~c_thread_pool()  
  180. {  
  181.     for (int i = 0; i < max_thread_num; ++i)   
  182.         pthread_cancel(threadid[i]);  
  183.     for (c_work *w_t = queue_head; w_t != NULL;)  
  184.     {  
  185.         c_work *temp = w_t->next;  
  186.         delete w_t;  
  187.         w_t = temp;  
  188.     }  
  189.     delete [] threadid;  
  190. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
线程池是一种常见的并发编程模型,它可以减少线程的创建和销毁带来的开销,提高多线程程序的性能和稳定性。下面是一个简单的C++线程池实现,仅供参考: ```c++ #include <iostream> #include <vector> #include <queue> #include <thread> #include <mutex> #include <condition_variable> #include <functional> #include <future> class ThreadPool { public: ThreadPool(size_t thread_count) : stop(false) { for (size_t i = 0; i < thread_count; ++i) { workers.emplace_back( [this] { for (;;) { std::function<void()> task; { std::unique_lock<std::mutex> lock(this->queue_mutex); this->condition.wait(lock, [this] { return this->stop || !this->tasks.empty(); }); if (this->stop && this->tasks.empty()) return; task = std::move(this->tasks.front()); this->tasks.pop(); } task(); } } ); } } template<class F, class... Args> auto enqueue(F&& f, Args&&... args) -> std::future<typename std::result_of<F(Args...)>::type> { using return_type = typename std::result_of<F(Args...)>::type; auto task = std::make_shared< std::packaged_task<return_type()> >( std::bind(std::forward<F>(f), std::forward<Args>(args)...) ); std::future<return_type> res = task->get_future(); { std::unique_lock<std::mutex> lock(queue_mutex); // 不允许向已停止的线程池中添加新的任务 if (stop) throw std::runtime_error("enqueue on stopped ThreadPool"); tasks.emplace([task]() { (*task)(); }); } condition.notify_one(); return res; } ~ThreadPool() { { std::unique_lock<std::mutex> lock(queue_mutex); stop = true; } condition.notify_all(); for (std::thread& worker : workers) worker.join(); } private: std::vector<std::thread> workers; std::queue<std::function<void()>> tasks; std::mutex queue_mutex; std::condition_variable condition; bool stop; }; ``` 上面的代码中,`ThreadPool`类封装了一个线程池,提供了`enqueue()`方法用于向线程池中添加任务。当调用`enqueue()`方法时,线程池会将任务封装成一个`std::packaged_task`对象,并将其放入任务队列中。工作线程会从任务队列中取出任务并执行,直到线程池被销毁或者调用`stop()`方法停止线程池。 可以使用下面的代码测试线程池的功能: ```c++ #include <chrono> #include <random> int main() { ThreadPool pool(4); std::vector<std::future<int>> results; std::random_device rd; std::mt19937 gen(rd()); std::uniform_int_distribution<> dis(1, 100); for (int i = 0; i < 8; ++i) { results.emplace_back( pool.enqueue([i, dis, &gen] { std::cout << "task " << i << " start" << std::endl; std::this_thread::sleep_for(std::chrono::seconds(dis(gen))); std::cout << "task " << i << " end" << std::endl; return i * i; }) ); } for (auto&& result : results) std::cout << result.get() << ' '; std::cout << std::endl; return 0; } ``` 上面的代码创建了一个包含4个线程的线程池,并向线程池中添加了8个任务。每个任务会随机休眠1~100秒的时间,然后返回任务编号的平方。最后打印所有任务的返回值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值