在C中链接任务指针

1 c pointers task threadpool

所以我试图在这里链接任务,但是在使用Linux下的GCC编译时,我收到警告:从不兼容的指针类型[默认启用]进行分配.即使我只是使用相同类型的指针.

typedef struct
{
    void (*routine)(void*);
    void* data;

    struct p_task* next;
    struct p_task* prev;

    int deadline;
    int timeWaiting;
}p_task;
typedef struct 
{   
    pthread_t mainThread;   

    pthread_t* threadArray;
    int threadCount;

    p_task* firstInLine;
    p_task* lastInLine;
}p_pool;


void pool_add_task(p_pool* pool, void* routine, void* data)
{   
    // create new task
    p_task* task = malloc(sizeof(p_task));
    task->routine = routine;
    task->data = data;
    task->deadline = 5;
    task->timeWaiting = 0;

    // when no tasks are chained yet
    if (pool->firstInLine == NULL) 
    {
        pool->firstInLine = task;
        pool->lastInLine = task;
    }
    else
    {       
        pool->lastInLine->next = task; // bad line 1
        task->prev = pool->lastInLine; // bad line 2
        pool->lastInLine = task; // new task is last in line
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 5

struct p_task因为你没有在结构的定义中使用它,所以这是因为是前向声明的.这意味着编译器不知道这一点

  1. 它确实存在

  2. 它与类型相同(typdef送入)p_task.

你需要写这个:

typedef struct p_task
{
    // etc.
} p_task;
Run Code Online (Sandbox Code Playgroud)