C 堆内存指针从结构内的指针字段丢失

C 堆内存指针从结构内的指针字段丢失,c,pthreads,posix,threadpool,C,Pthreads,Posix,Threadpool,我正在尝试使用POSIX线程库在C中实现线程池。在初始化线程池的函数中,对thpool和field jobqueue执行动态内存分配,但当我尝试从另一个函数访问jobqueue字段时,指针值为NULL。这是我的密码: threadpool.h threadpool.c 我的问题是,为什么即使我们在堆中动态分配内存,struct thpool的jobqueue字段的这个分配指针似乎也不会保持活动状态,尽管对于从函数createWorkers返回的thpool,我们仍然有它?如果问题出在thp->j

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

threadpool.h

threadpool.c

我的问题是,为什么即使我们在堆中动态分配内存,struct thpool的jobqueue字段的这个分配指针似乎也不会保持活动状态,尽管对于从函数createWorkers返回的thpool,我们仍然有它?如果问题出在thp->jobqueue=NULL,我肯定会错过一些东西。
–Ian Abbott

请尝试创建一个新的示例来向我们展示。请。如果在测试之间不编写大部分代码,调试就会变得容易得多。在只编写一小段代码之后,更频繁地进行测试。你肯定会错过缩进。保持活力你是如何诊断的?你这是什么意思?我喜欢这个设计,但还有一些工作要做。基本上,这只是消费者-生产者和单一生产者的问题。我相信当//*thp->jobqueue[*thp->jobtail].f=f;当createWorkers函数返回时,在主函数内我打印出了指针,它与createWorkers函数内分配的指针相同。我想分配给进程的堆在进程结束之前仍然存在,对吗?另外,我不想在这里使用全局变量来完成我的工作。如果问题是thp->jobqueue=NULL。
#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);
#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);
}