c++11 线程池系列之一 所需要的join_threads

class join_threads
{
std::vector<std::thread> &threads;
public:
	explicit join_threads(std::vector<std::thread> &threads_):threads(threads_){}
	~join_threads()
	{
	for(unsigned long i = 0 ; i < threads.size();++i)
	{
	if(threads[i].joinable())
		threads[i].join();
	}
	}
};

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的C语言线程池的代码实现: ```c #include <pthread.h> #include <stdlib.h> #include <stdio.h> #define DEFAULT_THREADS 4 typedef struct { void (*function)(void *); void *argument; } thread_task; typedef struct { thread_task *task_queue; int queue_size; int front; int rear; int count; pthread_mutex_t lock; pthread_cond_t not_empty; pthread_cond_t not_full; int threads_count; pthread_t *threads; int shutdown; } thread_pool; void *thread_pool_function(void *thread_pool_ptr) { thread_pool *pool = (thread_pool *) thread_pool_ptr; thread_task task; while (1) { pthread_mutex_lock(&(pool->lock)); while (pool->count == 0 && !pool->shutdown) { pthread_cond_wait(&(pool->not_empty), &(pool->lock)); } if (pool->shutdown) { pthread_mutex_unlock(&(pool->lock)); pthread_exit(NULL); } task = pool->task_queue[pool->front]; pool->front = (pool->front + 1) % pool->queue_size; pool->count--; if (pool->count == pool->queue_size - 1) { pthread_cond_broadcast(&(pool->not_full)); } pthread_mutex_unlock(&(pool->lock)); (*(task.function))(task.argument); } pthread_exit(NULL); } int thread_pool_create(thread_pool *pool, int threads_count) { int i; pool->threads_count = 0; pool->shutdown = 0; if (threads_count <= 0) { threads_count = DEFAULT_THREADS; } pool->queue_size = threads_count * 2; pool->front = 0; pool->rear = 0; pool->count = 0; pool->task_queue = (thread_task *) malloc(sizeof(thread_task) * pool->queue_size); pool->threads = (pthread_t *) malloc(sizeof(pthread_t) * threads_count); pthread_mutex_init(&(pool->lock), NULL); pthread_cond_init(&(pool->not_empty), NULL); pthread_cond_init(&(pool->not_full), NULL); for (i = 0; i < threads_count; i++) { pthread_create(&(pool->threads[i]), NULL, thread_pool_function, (void *) pool); pool->threads_count++; } return 0; } int thread_pool_add_task(thread_pool *pool, void (*function)(void *), void *argument) { thread_task task; pthread_mutex_lock(&(pool->lock)); while (pool->count == pool->queue_size && !pool->shutdown) { pthread_cond_wait(&(pool->not_full), &(pool->lock)); } if (pool->shutdown) { pthread_mutex_unlock(&(pool->lock)); return -1; } task.function = function; task.argument = argument; pool->task_queue[pool->rear] = task; pool->rear = (pool->rear + 1) % pool->queue_size; pool->count++; if (pool->count == 1) { pthread_cond_broadcast(&(pool->not_empty)); } pthread_mutex_unlock(&(pool->lock)); return 0; } int thread_pool_destroy(thread_pool *pool) { int i; if (pool->shutdown) { return -1; } pool->shutdown = 1; pthread_cond_broadcast(&(pool->not_empty)); pthread_cond_broadcast(&(pool->not_full)); for (i = 0; i < pool->threads_count; i++) { pthread_join(pool->threads[i], NULL); } free(pool->task_queue); free(pool->threads); pthread_mutex_destroy(&(pool->lock)); pthread_cond_destroy(&(pool->not_empty)); pthread_cond_destroy(&(pool->not_full)); return 0; } void task_function(void *argument) { int *value = (int *) argument; printf("Task %d\n", *value); } int main() { int i; int values[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; thread_pool pool; thread_pool_create(&pool, 4); for (i = 0; i < 10; i++) { thread_pool_add_task(&pool, task_function, &values[i]); } thread_pool_destroy(&pool); return 0; } ``` 该实现使用一个循环队列来存储线程任务,线程池的每个线程都会不断地从队列中取出任务并执行。线程池的创建、添加任务和销毁都是线程安全的,确保了多线程环境下线程池的正确性。在本例中,我们创建了一个包含10个任务的线程池,每个任务打印一个数字。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值