[英]memory leak when calling pthread_exit()
i need to create a thread pool it,it works but in function do_work that the function pthread_create calls i have problem in the free (memory leak) just when calling pthread_exit()我需要创建一个线程池,它可以工作,但是在函数 do_work 中,函数 pthread_create 调用我在调用 pthread_exit() 时在空闲(内存泄漏)中遇到问题
*in function create threadpool i just initilaize the struct and call function do work * *在函数中创建线程池我只是初始化结构和调用函数做工作*
void* do_work(void* p)
{
threadpool* pool = (threadpool*)p;
work_t* work;
while(1)
{
pthread_mutex_lock(&pool->qlock);
if(pool->shutdown == 1)
{
pthread_mutex_unlock(&pool->qlock);
//pthread_exit(EXIT_SUCCESS);// here is the free problem when deleting it all good
return NULL;
}
while(!pool->qsize)
{
if(pthread_cond_wait(&pool->q_not_empty,&pool->qlock))
perror("pthread_cond_wait\n");
if(pool->shutdown)
break;
}
//Check if the system is shutting down
if(pool->shutdown == 1)
{
pthread_mutex_unlock(&pool->qlock);
//pthread_exit(EXIT_SUCCESS);y
return NULL;
}
work = pool->qhead; //set the cur variable.
pool->qsize--; //decriment the size.
if(pool->qsize == 0) {
pool->qhead = NULL;
pool->qtail = NULL;
}
else {
pool->qhead = work->next;
}
if(pool->qsize == 0 && ! pool->shutdown) {
//the q is empty again, now signal that its empty.
pthread_cond_signal(&(pool->q_empty));
}
pthread_mutex_unlock(&(pool->qlock));
(work->routine) (work->arg); //actually do work.
free(work);
}
}
The pattern looks like a fairly standard thread pool:该模式看起来像一个相当标准的线程池:
typedef struct {
pthread_mutex_t qlock;
pthread_cond_t q_not_empty;
volatile work_t *qhead;
volatile work_t *qtail;
size_t qsize; /* Not needed */
volatile int shutdown;
} threadpool;
Properly indenting OP's code would make it more readable.正确缩进 OP 的代码会使其更具可读性。
However, the implementation looks odd.然而,实现看起来很奇怪。 I would expect
我希望
void *do_work(void *poolptr)
{
threadpool *const pool = poolptr;
work_t *work;
pthread_mutex_lock(&(pool->qlock));
while (!(pool->shutdown)) {
if (!(pool->qhead)) {
/* Queue empty */
pthread_cond_wait(&(pool->q_not_empty), &(pool->qlock));
continue;
}
work = pool->qhead;
pool->qhead = work->next;
if (!pool->qhead)
pool->qtail = NULL;
work->next = NULL;
pthread_unlock(&(pool->qlock));
work->process(work);
pthread_lock(&(pool->qlock));
}
pthread_mutex_unlock(&(pool->qlock));
return (void *)0;
}
and the code that appends a new work item to the queue to be以及将新工作项附加到队列的代码
void append(threadpool *pool, work_t *work)
{
work->next = NULL;
pthread_mutex_lock(&(pool->qlock));
if (pool->qtail) {
pool->qtail->next = work;
pool->qtail = work;
} else {
pool->qhead = work;
pool->qtail = work;
}
pthread_cond_signal(&(pool->q_not_empty));
pthread_mutex_unlock(&(pool->qlock));
}
It is difficult to say where OP's implementation leaks memory.很难说 OP 的实现在哪里泄漏了内存。 The most likely candidate is OP's
arg
member in each work_t
, if it is dynamically allocated.如果动态分配,最有可能的候选对象是每个
work_t
OP 的arg
成员。
My implementation above passes the entire work_t
to the routine
function, which I renamed to process
.我上面的实现将整个
work_t
传递给routine
函数,我将其重命名为process
。 It is responsible for freeing the work structure, too.它也负责释放工作结构。 The minimal definition for the work structure is
工作结构的最小定义是
typedef struct work_t {
struct work_t *next;
void (*process)(struct work_t *);
/* Optional other fields */
} work_t;
Other possible causes for the memory leaks is if the qsize
member is not properly updated everywhere.内存泄漏的其他可能原因是
qsize
成员没有在任何地方正确更新。 Because there is no use for it, really, I just omit it altogether.因为它没有用,真的,我只是完全省略了它。
The simpler the code, the easier it is to avoid bugs.代码越简单,越容易避免错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.