当前位置: 首页 > news >正文

工艺品东莞网站建设营销助手app

工艺品东莞网站建设,营销助手app,谁家网站做的好,网站做搜索关键字好吗创建一个线程池是提高多线程应用程序性能的有效方法。一个线程池中包含一定数量的工作线程#xff0c;这些线程可以复用来处理多个任务#xff0c;避免了频繁创建和销毁线程所带来的开销。 下面是一个基础的线程池实现的框架#xff0c;使用C语言和POSIX线程库(pthread)。这…创建一个线程池是提高多线程应用程序性能的有效方法。一个线程池中包含一定数量的工作线程这些线程可以复用来处理多个任务避免了频繁创建和销毁线程所带来的开销。 下面是一个基础的线程池实现的框架使用C语言和POSIX线程库(pthread)。这个简单的线程池会轮询并执行放入队列的任务。 #include stdio.h #include stdlib.h #include pthread.h #include unistd.h#define MAX_THREADS 4 #define MAX_QUEUE 8// 任务结构体 typedef struct {void (*function)(void *); // 任务的回调函数void *argument; // 回调函数的参数 } threadpool_task_t;// 线程池结构体 typedef struct {pthread_mutex_t lock; // 互斥锁pthread_cond_t notify; // 条件变量pthread_t *threads; // 线程数组threadpool_task_t *queue; // 任务队列int thread_count; int queue_size; int head; int tail; int count; // 当前任务数量int shutdown; // 是否关闭线程池int started; // 已启动的线程数量 } threadpool_t;// 线程工作函数 void *threadpool_thread(void *threadpool);// 创建线程池 threadpool_t *threadpool_create(int thread_count, int queue_size);// 向线程池中添加任务 int threadpool_add(threadpool_t *pool, void (*function)(void *), void *argument);// 销毁线程池 int threadpool_destroy(threadpool_t *pool, int flags);int main() {threadpool_t *pool threadpool_create(MAX_THREADS, MAX_QUEUE);// 添加一些示例任务for (int i 0; i 10; i) {threadpool_add(pool, my_task, (void *)(long)i);}sleep(3); // 等待任务完成threadpool_destroy(pool, 0);return 0; }// 任务示例 void my_task(void *arg) {printf(Thread %ld working on task %ld\n, pthread_self(), (long)arg); }threadpool_t *threadpool_create(int thread_count, int queue_size) {threadpool_t *pool (threadpool_t *)malloc(sizeof(threadpool_t));if (pool NULL) {return NULL;}pool-thread_count 0;pool-queue_size queue_size;pool-head pool-tail pool-count 0;pool-shutdown pool-started 0;pool-threads (pthread_t *)malloc(sizeof(pthread_t) * thread_count);pool-queue (threadpool_task_t *)malloc(sizeof(threadpool_task_t) * queue_size);pthread_mutex_init((pool-lock), NULL);pthread_cond_init((pool-notify), NULL);for (int i 0; i thread_count; i) {if (pthread_create((pool-threads[i]), NULL, threadpool_thread, (void *)pool) ! 0) {threadpool_destroy(pool, 0);return NULL;}pool-thread_count;pool-started;}return pool; }int threadpool_add(threadpool_t *pool, void (*function)(void *), void *argument) {int next, err 0;if (pthread_mutex_lock((pool-lock)) ! 0) {return -1;}next (pool-tail 1) % pool-queue_size;do {if (pool-count pool-queue_size) {err 1;break;}pool-queue[pool-tail].function function;pool-queue[pool-tail].argument argument;pool-tail next;pool-count 1;if (pthread_cond_signal((pool-notify)) ! 0) {err 1;break;}} while (0);if (pthread_mutex_unlock((pool-lock)) ! 0) {err 1;}return err; }int threadpool_destroy(threadpool_t *pool, int flags) {if (pool NULL) {return -1;}if (pthread_mutex_lock((pool-lock)) ! 0) {return -1;}pool-shutdown 1;if ((pthread_cond_broadcast((pool-notify)) ! 0) || (pthread_mutex_unlock((pool-lock)) ! 0)) {return -1;}for (int i 0; i pool-thread_count; i) {if (pthread_join(pool-threads[i], NULL) ! 0) {return -1;}}if (pthread_mutex_destroy((pool-lock)) ! 0 || pthread_cond_destroy((pool-notify)) ! 0) {return -1;}free(pool-threads);free(pool-queue);free(pool);return 0; }void *threadpool_thread(void *threadpool) {threadpool_t *pool (threadpool_t *)threadpool;threadpool_task_t task;while (1) {pthread_mutex_lock((pool-lock));while ((pool-count 0) (!pool-shutdown)) {pthread_cond_wait((pool-notify), (pool-lock));}if ((pool-shutdown) (pool-count 0)) {break;}task.function pool-queue[pool-head].function;task.argument pool-queue[pool-head].argument;pool-head (pool-head 1) % pool-queue_size;pool-count - 1;pthread_mutex_unlock((pool-lock));(*(task.function))(task.argument);}pool-started--;pthread_mutex_unlock((pool-lock));pthread_exit(NULL);return NULL; }代码说明 结构体定义: threadpool_task_t: 表示一个任务包括任务的回调函数和参数。threadpool_t: 表示线程池包括线程数组、任务队列、队列头尾索引、互斥锁、条件变量等。 线程池工作函数threadpool_thread: 线程从任务队列中获取任务并执行。在任务队列为空时该线程将会阻塞等待新的任务。 线程池创建函数threadpool_create: 初始化线程池的结构体各种初始值和资源分配。创建指定数量的线程并将它们与threadpool_thread函数关联起来。 添加任务函数threadpool_add: 将任务放入队列并通知一个阻塞在条件变量上的线程去执行这个任务。 销毁线程池函数threadpool_destroy: 关闭线程池等待所有线程终止并销毁分配的资源。 示例任务my_task: 简单输出线程ID和任务ID示例。 这个基础代码可以满足大部分简单的线程池需求但在实际应用中你可能还会需要考虑 线程池中的异常处理。动态调整线程池大小。更复杂的任务调度和优先级。更高效的任务队列实现。 这个基础代码是一个很好的起点你可以在其基础上根据具体需求做进一步优化和扩展。
http://www.pierceye.com/news/822209/

相关文章:

  • 网站广告收入如何缴文化事业建设费东莞人才市场招聘信息2023
  • 各大网站网址目录云服务器是什么意思
  • 免费作文网站张家港做网站排名
  • 具有营销价值好的网站找网站建设需要问什么软件
  • 免费好用的网页制作软件网站建设优化开发公司
  • 跟我一起做网站pdf电驴建设网站公开教学视频
  • 广州网站制作商怎样查看网站的权重
  • 网站群建设标准最好的响应式网站
  • 石家庄市建设厅网站网页网页设计班
  • 网站首页怎么设计公司网页制作培训试题
  • 虚拟主机怎么做网站新seo排名点击软件
  • 买服务器做网站 镜像选什么网页设计用的软件
  • 内江 网站建设网站后台登录地址
  • 网站建设合同副本临沂建设局官方网站
  • 林州网站建设哪家好网站做电子链接标识申请好吗
  • 石家庄开发网站建设263企业邮箱登录邮箱
  • 青岛公司建设网站电子商务网站开发技术论文
  • 龙华网站的建设建设网站教程视频视频
  • 威海高区建设局网站品牌网站建设多少钱
  • 网站php网站空间新乡河南网站建设
  • 你第一个物流网站建设方案信诚网络公司网站
  • 企业建站什么网站好wordpress管理员后台
  • 南京网站开发价格两个wordpress共用一个数据库
  • 番禺制作网站平台柳城网站制作
  • 网站建设相关视频教程网页设计布局有哪几种方法
  • 能接做网站的活的网站上海建网站方案
  • 免费网站软件app大全飘雪影院手机免费观看免费
  • 怎么做类似淘宝的网站石家庄网络开发公司
  • 专业SEO教程网站广东省住房与城乡建设部网站
  • 360免费建站为什么注册不了雨灿网站建设