【问题标题】:Chaining task pointers in C在 C 中链接任务指针
【发布时间】:2013-02-08 22:36:37
【问题描述】:

所以我试图在这里链接任务,但是在 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
    }
}

【问题讨论】:

  • 消息对应哪一行?
  • 我在坏线后面放了 2 个 cmets,主要是在使用 prev 或 next 时。

标签: c pointers task threadpool


【解决方案1】:

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

  1. 确实存在

  2. 它与(typdeffed to)p_task 类型相同。

你需要这样写:

typedef struct p_task
{
    // etc.
} p_task;

【讨论】:

    猜你喜欢
    • 2018-04-16
    • 1970-01-01
    • 2018-07-28
    • 2016-02-08
    • 2022-01-22
    • 1970-01-01
    • 2013-08-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多