gpt4 book ai didi

c - 堆内存指针从结构内部的指针字段丢失

转载 作者:行者123 更新时间:2023-11-30 14:42:07 26 4
gpt4 key购买 nike

我正在尝试使用 POSIX 线程库在 C 中实现线程池。在初始化线程池的函数中,为 thpool 和字段 jobqueue 执行动态内存分配,但是当我尝试从另一个函数访问 jobqueue 字段时,指针值为 <强>NULL。这是我的代码:

threadpool.h

#include <pthread.h>

typedef void *(*funcptr) (void *);

typedef enum JOBSTATUS
{
COMPLETED,
RUNNING,
PENDING
} jobstatus;

typedef struct _job
{
funcptr f;
void *args;
} job;

typedef struct _thpool
{
job *jobqueue;
int jobquesize;
int jobtail;
int jobhead;
int occupiedjobs;

pthread_t *threadqueue;
int nofthreads;
int threadsOccupied;
pthread_mutex_t mtx;
pthread_cond_t q_notempty;
pthread_cond_t q_empty;
} thpool;

thpool *createWorkers(int numofthreads, int nofjobs);

threadpool.c

#include "threadpool.h"
#include <stdlib.h>
#include <stdio.h>

void *primetest(void *args)
{
int *k = (int *)args;
int i, counter;
for (i = 0; i <= *k; i++)
{
if (*k % i == 0)
counter++;
}
return counter >= 3 ? (void *)0 : (void *)1;
}

void *dothreadWork(void *args)
{
thpool *thp = (thpool *)args;
funcptr f;
void *arg;
while (1)
{
pthread_mutex_lock(&(thp->mtx));
while (thp->occupiedjobs == 0 /*&& thp->jobtail==thp->jobhead */ )
{
pthread_cond_wait(&thp->q_notempty, &thp->mtx);
}
f = thp->jobqueue[thp->jobhead].f;
arg = thp->jobqueue[thp->jobhead].args;
--thp->occupiedjobs;
//if(thp->jobquesize==0)
pthread_mutex_unlock(&(thp->mtx));
f(arg);
}
}

thpool *createWorkers(int numofthreads, int nofjobs)
{
int i;
thpool *thp = (thpool *)malloc(sizeof(thpool));
if (thp == NULL)
{
return NULL;
}
thp->jobqueue = (job *)malloc(sizeof(job) * nofjobs);
if (thp->jobqueue = NULL)
{
free(thp);
return NULL;
}
thp->threadqueue = (pthread_t *)malloc(sizeof(pthread_t) * numofthreads);
if (thp->threadqueue == NULL)
{
free(thp->jobqueue);
free(thp);
return NULL;
}
pthread_mutex_init(&thp->mtx, 0);
pthread_cond_init(&thp->q_notempty, NULL);
pthread_cond_init(&thp->q_empty, NULL);
thp->jobquesize = nofjobs;
thp->jobtail = thp->jobhead = 0;
thp->occupiedjobs = 0;
thp->threadsOccupied = 0;
thp->nofthreads = numofthreads;
for (i = 0; i < numofthreads; i++)
{
if (pthread_create(&(thp->threadqueue[i]), NULL, dothreadWork, thp) > 0)
{
return NULL;
}
}
return thp;
}
void jobqueuepush(thpool **thp, funcptr f, void *args)
{
printf("Jobqueue Push!");
pthread_mutex_lock(&((*thp)->mtx));
if ((*thp)->jobquesize == (*thp)->occupiedjobs)
{
pthread_mutex_unlock(&((*thp)->mtx));
return;
}
if ((*thp)->jobqueue == NULL) // The problem is here
{
pthread_mutex_unlock(&((*thp)->mtx));
printf("Problem in memory allocation\n");
return;
}
//(*thp)->jobqueue[(*thp)->jobtail].f = f;
//(*thp)->jobqueue[(*thp)->jobtail].args = args;
//++(*thp)->occupiedjobs;
(*thp)->jobtail = ((*thp)->jobtail + 1) % ((*thp)->jobquesize);
if ((*thp)->occupiedjobs == 1)
pthread_cond_signal(&((*thp)->q_notempty));
pthread_mutex_unlock(&((*thp)->mtx));

}

int main(int argc, void *argv[])
{
thpool *p = createWorkers(4, 2);

if (p != NULL)
{
int k = 4;
jobqueuepush(&p, primetest, &k);
}
return (EXIT_SUCCESS);
}

我的问题是,为什么即使我们在堆中动态分配内存,struct thpooljobqueue 字段的分配指针似乎也没有保持事件状态,尽管对于 从函数 createWorkers 返回的 thpool 我们还有吗?我肯定想念这里的一些东西。

最佳答案

if (thp->jobqueue = NULL) 是问题所在。 – 伊恩·阿博特

关于c - 堆内存指针从结构内部的指针字段丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54569590/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com