本篇文章我们来讲一下linux进程 线程 线程池的内容
以下是linux线程池使用的思维导图:
Linux进程是程序执行的一个实例,是系进统行资源分配和调度的基本单位。进程是动态的,有生命周期及运行状态,是程序运行的动态过程。
Linux线程池的介绍:
Linux线程池(Thread Pool)是一种线程使用模式,它预先创建并维护多个线程,这些线程等待任务到来时,从线程池中选取一个线程来执行任务。线程池通过减少线程的创建和销毁次数,降低了开销,提高了系统的响应速度和吞吐量。
在Linux环境下,线程池的实现通常依赖于系统提供的线程库,如pthread。线程池中的线程在初始状态下可能处于休眠状态,不消耗CPU,但占用较小的内存空间。当新任务到来时,线程池管理器会选择一个空闲线程来执行任务。如果线程池中没有空闲线程,线程池管理器会新建一定数量的线程来处理任务。当系统比较空闲时,线程池管理器会自动销毁一部分线程,回收系统资源。
线程池的优点包括:
- 降低开销:通过复用线程,避免了频繁地创建和销毁线程,减少了系统开销。
- 提高性能:线程池中的线程可以被多个任务共享,提高了系统的并发处理能力。
- 简化编程:线程池将线程的创建、管理和销毁等底层细节封装起来,简化了开发者的编程工作。
在Linux中,线程池通常用于处理大量并发任务,如WEB服务器处理网页请求等场景。通过合理使用线程池,可以显著提高系统的性能和稳定性。
linux线程池的创建和使用实例:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#define MAX_THREADS 10
#define QUEUE_SIZE 100
typedef struct task {
void (*func)(void* arg);
void* arg;
struct task* next;
} task_t;
typedef struct thread_pool {
pthread_mutex_t lock;
pthread_cond_t cond;
int thread_count;
int task_count;
task_t* task_queue;
pthread_t* threads;
} thread_pool_t;
void* worker(void* arg) {
thread_pool_t* pool = (thread_pool_t*) arg;
while (1) {
pthread_mutex_lock(&pool->lock);
while (pool->task_count == 0) {
pthread_cond_wait(&pool->cond, &pool->lock);
}
task_t* task = pool->task_queue;
pool->task_queue = task->next;
pool->task_count--;
pthread_mutex_unlock(&pool->lock);
task->func(task->arg);
free(task);
}
return NULL;
}
thread_pool_t* create_pool(int num_threads) {
thread_pool_t* pool = (thread_pool_t*) malloc(sizeof(thread_pool_t));
pool->task_queue = NULL;
pool->task_count = 0;
pool->thread_count = 0;
pool->threads = (pthread_t*) malloc(num_threads * sizeof(pthread_t));
pthread_mutex_init(&pool->lock, NULL);
pthread_cond_init(&pool->cond, NULL);
for (int i = 0; i < num_threads; i++) {
pthread_create(&pool->threads[i], NULL, worker, pool);
pool->thread_count++;
}
return pool;
}
void add_task(thread_pool_t* pool, void (*func)(void* arg), void* arg) {
task_t* task = (task_t*) malloc(sizeof(task_t));
task->func = func;
task->arg = arg;
task->next = NULL;
pthread_mutex_lock(&pool->lock);
if (pool->task_count == QUEUE_SIZE) {
fprintf(stderr, "Error: task queue is full\n");
pthread_mutex_unlock(&pool->lock);
free(task);
return;
}
if (pool->task_queue == NULL) {
pool->task_queue = task;
} else {
task_t* last = pool->task_queue;
while (last->next != NULL) {
last = last->next;
}
last->next = task;
}
pool->task_count++;
pthread_cond_signal(&pool->cond);
pthread_mutex_unlock(&pool->lock);
}
void destroy_pool(thread_pool_t* pool) {
pthread_mutex_lock(&pool->lock);
while (pool->task_count > 0) {
pthread_cond_wait(&pool->cond, &pool->lock);
}
pthread_mutex_unlock(&pool->lock);
for (int i = 0; i < pool->thread_count; i++) {
pthread_join(pool->threads[i], NULL);
}
free(pool->threads);
pthread_mutex_destroy(&pool->lock);
pthread_cond_destroy(&pool->cond);
free(pool);
}
void print_num(void* arg) {
int num = *(int*) arg;
printf("%d\n", num);
}
int main() {
thread_pool_t* pool = create_pool(MAX_THREADS);
for (int i = 0; i < 20; i++) {
int* num = (int*) malloc(sizeof(int));
*num = i;
add_task(pool, print);
}
总结:本篇文章主要是讲述了linux线程池的概念和使用 线程池是大量线程的聚集体 能够优化性能 减少耗时等等
好了 本篇文章就到这里结束了 在这里我想向大家推荐一个课程: