pthread_mutex_cond_lock_full

本文探讨了在使用pthread_cond_wait时遇到的错误,并分析了导致错误的原因,即mutex状态被破坏。文章通过观察和分析,指出主线程退出时,mutex对象被析构而此时恰好有线程在使用它,从而导致程序崩溃。最后提供了解决方案,帮助开发者避免此类问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上周使用pthread_cond_wait时遇到一个从来没见过的错误,程序core在这里:

../nptl/pthread_mutex_lock.c:350: __pthread_mutex_cond_lock_full: Assertion `(-(e)) != 3 || !robust' failed.

经过观察和分析,推断是mutex的状态被破坏了。
排查代码,主线程退出时,mutex对象被析构,些时恰好有一个纯程在使用它,于是就core了。

转载于:https://www.cnblogs.com/basalt/p/4598006.html

#include "thread_pool.h" #include <stdlib.h> #include <stdio.h> void queue_init(task_queue_t *queue, int capacity) { queue->tasks = malloc(capacity * sizeof(task_t)); queue->front = 0; queue->rear = -1; queue->size = 0; queue->capacity = capacity; } void queue_push(task_queue_t *queue, task_t task) { if (queue->size >= queue->capacity) { fprintf(stderr, "Task queue is full\n"); return; } queue->rear = (queue->rear + 1) % queue->capacity; queue->tasks[queue->rear] = task; queue->size++; } task_t queue_pop(task_queue_t *queue) { if (queue->size <= 0) { fprintf(stderr, "Task queue is empty\n"); task_t empty_task = {NULL, NULL}; return empty_task; } task_t task = queue->tasks[queue->front]; queue->front = (queue->front + 1) % queue->capacity; queue->size--; return task; } int queue_empty(task_queue_t *queue) { return queue->size == 0; } void queue_clean(task_queue_t *queue) { free(queue->tasks); queue->tasks = NULL; queue->front = 0; queue->rear = -1; queue->size = 0; queue->capacity = 0; } void *worker_thread(void *arg) { thread_pool_t *pool = (thread_pool_t *)arg; while (1) { pthread_mutex_lock(&pool->lock); // 等待新任务或关闭信号 while (queue_empty(&pool->queue) && !pool->shutdown) { pthread_cond_wait(&pool->cond, &pool->lock); } // 检查是否关闭 if (pool->shutdown) { pthread_mutex_unlock(&pool->lock); pthread_exit(NULL); } // 获取任务 task_t task = queue_pop(&pool->queue); pthread_mutex_unlock(&pool->lock); // 执行任务 if (task.function) { task.function(task.arg); } } return NULL; } thread_pool_t *thread_pool_create(int num_threads) { thread_pool_t *pool = malloc(sizeof(thread_pool_t)); pool->num_threads = num_threads; pool->shutdown = 0; // 初始化任务队列 queue_init(&pool->queue, 128); // 初始化互斥锁和条件变量 pthread_mutex_init(&pool->lock, NULL); pthread_cond_init(&pool->cond, NULL); // 创建工作线程 pool->threads = malloc(num_threads * sizeof(pthread_t)); for (int i = 0; i < num_threads; i++) { pthread_create(&pool->threads[i], NULL, worker_thread, pool); } return pool; } void thread_pool_add_task(thread_pool_t *pool, void (*function)(void *), void *arg) { pthread_mutex_lock(&pool->lock); // 创建新任务 task_t task; task.function = function; task.arg = arg; // 添加到队列 queue_push(&pool->queue, task); // 通知工作线程 pthread_cond_signal(&pool->cond); pthread_mutex_unlock(&pool->lock); } void thread_pool_destroy(thread_pool_t *pool) { pthread_mutex_lock(&pool->lock); pool->shutdown = 1; pthread_cond_broadcast(&pool->cond); pthread_mutex_unlock(&pool->lock); // 等待所有线程退出 for (int i = 0; i < pool->num_threads; i++) { pthread_join(pool->threads[i], NULL); } // 清理资源 free(pool->threads); queue_clean(&pool->queue); pthread_mutex_destroy(&pool->lock); pthread_cond_destroy(&pool->cond); free(pool); }
最新发布
08-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值