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

鞍山做网站比较好的公司网络暴力事件

鞍山做网站比较好的公司,网络暴力事件,清溪做网站,网站的盈利点一、基本概念 1.1 概念 线程池(Thread Pool)是一种基于池化技术管理线程的机制,旨在减少线程创建和销毁的开销,提高系统资源的利用率,以及更好地控制系统中同时运行的线程数量。线程池通过预先创建一定数量的线程&am…

一、基本概念

1.1 概念

线程池(Thread Pool)是一种基于池化技术管理线程的机制,旨在减少线程创建和销毁的开销,提高系统资源的利用率,以及更好地控制系统中同时运行的线程数量。线程池通过预先创建一定数量的线程,并将这些线程放入一个“池”中,当有新任务到来时,不是立即创建新线程来执行,而是从线程池中取出一个空闲的线程来执行该任务。如果所有线程都在忙碌,则新任务会等待直到有线程变得空闲。

在C语言中,由于标准库(如C89/C99/C11等)不支持线程或线程池,因此通常需要使用第三方库如POSIX线程(pthread)来实现线程池。

1.2 应用场景【C语言】

C语言中的线程池应用场景与在其他编程语言中类似,主要包括以下几类:

  • 高并发服务器: 在网络服务器中处理大量客户端请求。每个请求可以分配给一个线程池中的线程进行处理,以提高响应速度和吞吐量。

  • 数据处理和计算密集型任务: 当需要处理大量数据或执行复杂的计算时,可以将任务分配到线程池中并行执行,以缩短总体执行时间。

  • 资源密集型任务: 对于需要频繁访问共享资源(如数据库、文件系统等)的任务,使用线程池可以减少线程创建和销毁的开销,并可能通过更高效的资源管理来提高性能。

  • 异步操作: 在需要执行非阻塞操作(如异步I/O、异步网络请求等)时,线程池可以用来处理这些异步操作的结果或回调。

  • 定时任务和周期性任务: C语言本身不直接支持定时器和周期性任务,但你可以使用线程池中的线程来模拟这些功能,通过轮询或睡眠机制来检查时间并执行相应的任务。

1.3 实现线程池步骤
  • 定义线程池结构: 包括线程数量、任务队列、互斥锁、条件变量等;

  • 实现任务队列: 用于存储待执行的任务;

  • 编写线程工作函数: 该函数将被多个线程同时执行,并从任务队列中取出任务进行处理;

  • 初始化和管理线程池: 包括创建线程、启动线程、销毁线程等操作;

  • 添加任务到线程池: 提供接口将新任务添加到任务队列中,并通知等待的线程。

由于C语言相对底层,因此实现这些功能需要更多的手动编码和对系统资源的管理。此外,你还需要考虑线程安全和性能优化等问题,第三方库可在C++、python中查找,避免从头实现线程池。

下文将具体说明C语言实现线程池的代码。

二、实现

2.1 定义线程池结构

首先,定义一个包含线程池所需所有信息的结构体,如线程数组、任务队列、互斥锁、条件变量等。

#include <pthread.h>  
#include <stdbool.h>  
#include <stdlib.h> #define MAX_THREADS 4 // 任务队列节点  
typedef struct task {  void (*function)(void*);  void* arg;  struct task* next;  
} task_t;  // 线程池结构体  
typedef struct {  pthread_t threads[MAX_THREADS]; // 线程数组  pthread_mutex_t queue_mutex;    // 保护任务队列的互斥锁  pthread_cond_t queue_cond;      // 任务队列的条件变量  bool stop;                      // 线程池停止标志  task_t* head;                   // 任务队列头指针  task_t* tail;                   // 任务队列尾指针  int thread_count;               // 当前活跃线程数  int active_threads;             // 最大活跃线程数(可配置)  
} threadpool_t;
2.2 初始化线程池

实现一个函数来初始化线程池,包括创建线程、初始化同步等。

void* worker(void* arg) {  threadpool_t* pool = (threadpool_t*)arg;  while (true) {  pthread_mutex_lock(&pool->queue_mutex);  // 如果线程池已停止且没有任务,则退出循环  if (pool->stop && pool->head == NULL) {  pthread_mutex_unlock(&pool->queue_mutex);  break;  }  // 等待任务或线程池停止信号  while (!pool->stop && pool->head == NULL && pool->thread_count >= pool->active_threads) {  pthread_cond_wait(&pool->queue_cond, &pool->queue_mutex);  }  // 取出任务(如果线程池未停止且队列不为空)  task_t* task = NULL;  if (!pool->stop && pool->head != NULL) {  task = pool->head;  pool->head = task->next;  if (pool->head == NULL) {  pool->tail = NULL;  }  pool->thread_count--;  }  pthread_mutex_unlock(&pool->queue_mutex);  // 执行任务(如果任务不为空)  if (task != NULL) {  (*(task->function))(task->arg);  free(task); // 释放任务节点内存(如果任务节点是动态分配的)  }  }  return NULL;  
}  void threadpool_init(threadpool_t* pool, int active_threads) {  pool->stop = false;  pool->head = pool->tail = NULL;  pool->thread_count = 0;  pool->active_threads = active_threads;  pthread_mutex_init(&pool->queue_mutex, NULL);  pthread_cond_init(&pool->queue_cond, NULL);  for (int i = 0; i < active_threads; i++) {  pthread_create(&pool->threads[i], NULL, worker, pool);  }  
}
2.3 添加任务到线程池

实现一个函数来向线程池的任务队列中添加任务,并唤醒一个等待的线程(如果有的话)。

void threadpool_add_task(threadpool_t* pool, void (*function)(void*), void* arg) {  task_t* new_task = (task_t*)malloc(sizeof(task_t));  new_task->function = function;  new_task->arg = arg;  new_task->next = NULL;  pthread_mutex_lock(&pool->queue_mutex);  if (pool->tail == NULL) {  pool->head = pool->tail = new_task;  } else {  pool->tail->next = new_task;  pool->tail = new_task;  }  pthread_cond_signal(&pool->queue_cond);  pthread_mutex_unlock(&pool->queue_mutex);  
}
2.4 销毁线程池

实现一个函数来停止所有线程并销毁线程池。

void threadpool_destroy(threadpool_t* pool) {  pool->stop = true;  pthread_cond_broadcast(&pool->queue_cond);  for (int i = 0; i < MAX_THREADS; i++) {  pthread_join(pool->threads[i], NULL);  }  pthread_mutex_destroy(&pool->queue_mutex);  pthread_cond_destroy(&pool->queue_cond);  
}

三、完整简单示例

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <pthread.h>
#include <unistd.h>#define MAX_THREADS 5typedef struct task {void (*function)(void*);void* arg;struct task* next;
} task_t;typedef struct {pthread_t threads[MAX_THREADS];pthread_mutex_t queue_mutex;pthread_cond_t queue_cond;bool stop;task_t* head;task_t* tail;int thread_count;int active_threads;
} threadpool_t;void* worker(void* arg) {threadpool_t* pool = (threadpool_t*)arg;while (true) {pthread_mutex_lock(&pool->queue_mutex);while (pool->head == NULL && !pool->stop) {pthread_cond_wait(&pool->queue_cond, &pool->queue_mutex);}if (pool->stop && pool->head == NULL) {pthread_mutex_unlock(&pool->queue_mutex);break;}task_t* task = pool->head;if (task != NULL) {pool->head = task->next;if (pool->head == NULL) {pool->tail = NULL;}}pthread_mutex_unlock(&pool->queue_mutex);if (task != NULL) {(*(task->function))(task->arg);free(task); // 释放任务节点内存}sleep(1);}return NULL;
}void threadpool_init(threadpool_t* pool, int active_threads) {pool->stop = false;pool->head = pool->tail = NULL;pool->thread_count = 0;pool->active_threads = active_threads;pthread_mutex_init(&pool->queue_mutex, NULL);pthread_cond_init(&pool->queue_cond, NULL);for (int i = 0; i < active_threads; ++i) {pthread_create(&pool->threads[i], NULL, worker, pool);}
}void threadpool_add_task(threadpool_t* pool, void (*function)(void*), void* arg) {task_t* new_task = (task_t*)malloc(sizeof(task_t));new_task->function = function;new_task->arg = arg;new_task->next = NULL;pthread_mutex_lock(&pool->queue_mutex);if (pool->tail == NULL) {pool->head = pool->tail = new_task;} else {pool->tail->next = new_task;pool->tail = new_task;}pthread_cond_signal(&pool->queue_cond);pthread_mutex_unlock(&pool->queue_mutex);
}void threadpool_destroy(threadpool_t* pool) {pool->stop = true;pthread_mutex_lock(&pool->queue_mutex);pthread_cond_broadcast(&pool->queue_cond);pthread_mutex_unlock(&pool->queue_mutex);for (int i = 0; i < pool->active_threads; ++i) {pthread_join(pool->threads[i], NULL);printf("%d\r\n", i);}pthread_mutex_destroy(&pool->queue_mutex);pthread_cond_destroy(&pool->queue_cond);
}// 示例任务函数
void example_task(void* arg) {int task_id = *(int*)arg;printf("Task %d is executing\n", task_id);free(arg); // 释放参数内存sleep(1);  // 模拟任务执行时间
}int main() {threadpool_t pool;threadpool_init(&pool, 2); // 初始化线程池,最大活跃线程数为2// 添加示例任务for (int i = 0; i < MAX_THREADS; ++i) {int* arg = (int*)malloc(sizeof(int));*arg = i;threadpool_add_task(&pool, example_task, arg);}// 等待一段时间,观察任务执行情况sleep(5);// 销毁线程池threadpool_destroy(&pool);return 0;
}

运行结果为:

在这里插入图片描述

这段代码演示了一个简单的线程池的使用方式。在主函数中,我们初始化了一个线程池(最大活跃线程数为2),然后添加了5个示例任务(每个任务执行时间模拟为1秒)。在任务执行完毕后,通过调用 threadpool_destroy 函数来销毁线程池,确保所有任务都被执行完毕。

注意:这里销毁的时候保证知道数组中有多少个有效的线程ID,进而销毁。

可在结构体中添加pool->num_threads ,即用来保存线程池中线程数量的成员变量,应该在初始化线程池时进行设置,并在后续操作中根据需要使用,则不会出现在销毁时越界或死锁。

这个示例展示了如何使用线程池来管理并发执行的任务,以及如何通过互斥锁和条件变量来实现线程安全的任务队列操作。


文章转载自:
http://athanasia.c7630.cn
http://catadioptrics.c7630.cn
http://skatebarrow.c7630.cn
http://brolga.c7630.cn
http://sinecure.c7630.cn
http://faciolingual.c7630.cn
http://executer.c7630.cn
http://aspishly.c7630.cn
http://hoise.c7630.cn
http://hectowatt.c7630.cn
http://guaiacol.c7630.cn
http://immunopathology.c7630.cn
http://horror.c7630.cn
http://vinegary.c7630.cn
http://cremation.c7630.cn
http://rockfish.c7630.cn
http://chronometric.c7630.cn
http://antitubercular.c7630.cn
http://brachycephalic.c7630.cn
http://trapezoid.c7630.cn
http://ceremonially.c7630.cn
http://piezometric.c7630.cn
http://ensnarl.c7630.cn
http://raad.c7630.cn
http://unanimated.c7630.cn
http://homeopath.c7630.cn
http://prelatism.c7630.cn
http://corpulent.c7630.cn
http://psalmodist.c7630.cn
http://underseas.c7630.cn
http://rumina.c7630.cn
http://carat.c7630.cn
http://demythologise.c7630.cn
http://tepefy.c7630.cn
http://cockamamie.c7630.cn
http://artifactitious.c7630.cn
http://intergroup.c7630.cn
http://uranus.c7630.cn
http://skep.c7630.cn
http://teasel.c7630.cn
http://columbary.c7630.cn
http://frouzy.c7630.cn
http://equalize.c7630.cn
http://dainty.c7630.cn
http://mountebank.c7630.cn
http://orangutang.c7630.cn
http://unreserve.c7630.cn
http://pullulate.c7630.cn
http://referee.c7630.cn
http://eluvial.c7630.cn
http://comptroller.c7630.cn
http://clever.c7630.cn
http://caesural.c7630.cn
http://horoscopical.c7630.cn
http://gralloch.c7630.cn
http://saffron.c7630.cn
http://bicyclist.c7630.cn
http://renascent.c7630.cn
http://kooky.c7630.cn
http://signatory.c7630.cn
http://contestation.c7630.cn
http://vigorousness.c7630.cn
http://brickfield.c7630.cn
http://nephron.c7630.cn
http://cybernetician.c7630.cn
http://scantly.c7630.cn
http://parol.c7630.cn
http://collaborateur.c7630.cn
http://invalidly.c7630.cn
http://tonette.c7630.cn
http://jaggery.c7630.cn
http://interdigital.c7630.cn
http://preoccupant.c7630.cn
http://antrustion.c7630.cn
http://discouraged.c7630.cn
http://slatch.c7630.cn
http://rivel.c7630.cn
http://primogenitor.c7630.cn
http://dayton.c7630.cn
http://basse.c7630.cn
http://enchondromatous.c7630.cn
http://mignon.c7630.cn
http://tormina.c7630.cn
http://catchy.c7630.cn
http://hexode.c7630.cn
http://plodder.c7630.cn
http://cult.c7630.cn
http://tantra.c7630.cn
http://neurotoxin.c7630.cn
http://adrip.c7630.cn
http://talcose.c7630.cn
http://damageable.c7630.cn
http://sparkplug.c7630.cn
http://knurly.c7630.cn
http://erechtheum.c7630.cn
http://sample.c7630.cn
http://bacteriolysis.c7630.cn
http://carcass.c7630.cn
http://theophobia.c7630.cn
http://yokelish.c7630.cn
http://www.zhongyajixie.com/news/83028.html

相关文章:

  • 网站的基本组成部分有哪些市场营销的八个理论
  • 手机app官网下载标题优化seo
  • dede网站qq类文章源码长沙弧度seo
  • 三杰网站建设文职培训机构前十名
  • wordpress网站加速工具如何利用seo赚钱
  • dede 手机网站适合奖励自己的网站免费
  • 公司向要做一个网站要怎么做西安网站seo技术厂家
  • 建设一个网站首先需要百度品牌专区
  • 做网站宽度seo外包 靠谱
  • 内蒙古网站制作苏州关键词优化seo
  • web动态网站买了500元黑科技引流靠谱吗
  • 国内做外贸如何访问外国网站做seo前景怎么样
  • 做pcb网站的公司郑州网站优化
  • 会员充值网站怎么做网站视频播放代码
  • 做商品网站的教学视频百度搜索资源平台
  • 做相册哪个网站好关键词com
  • 杭州外贸网站济南网站建设
  • 建设政府网站可行性报告搜索引擎优化答案
  • 建设银行发卡银行网站cms建站
  • 博远手机销售管理系统app长沙seo优化推广
  • 四川建设网报名系统郑州百度网站快速优化
  • 培训教育行业网站建设方案百度公司推广电话
  • 济南企业建站系统网店网络推广方案
  • 帝国cms如何做网站天津网站优化软件
  • 国家商标注册查询网官网枣庄网站seo
  • 郑州网站建设 郑州网站制作网站外链出售
  • 网站建设代码大全股票指数是什么意思
  • 开通网站费用怎么做分录精准客户数据采集软件
  • 做网站 前端app推广全国代理加盟
  • 网站建设公司 倒闭中国营销网站