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

时间:2019-02-07 08:59:17

标签: c pthreads posix threadpool

我正在尝试使用POSIX线程库在C中实现线程池。在初始化线程池的函数中,同时为 thpool 和字段 jobqueue 执行动态内存分配,但是当我尝试从另一个函数访问jobqueue字段时,指针值为< strong> 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 thpool jobqueue 字段,该分配指针似乎仍然不活动函数 createWorkers 返回的thpool ,我们还有吗?我肯定会在这里想念的东西。

1 个答案:

答案 0 :(得分:1)

if (thp->jobqueue = NULL)是问题所在。                     –伊恩·雅培