gpt4 book ai didi

c - 在c中将队列作为参数传递

转载 作者:行者123 更新时间:2023-11-30 18:59:30 25 4
gpt4 key购买 nike

我在源文件 a.c 和 b.c 之间传递这样的队列

文件:a.c

sq[a]=new_queue();

pthread_create(&st[a],NULL,sendPacket,sq[a]);

文件:b.c

void *sendPacket(void *queue){

/* here i need to know which queue has come ,determine
the index of queue how can I do it? */
}

最佳答案

创建队列的更高级表示。看起来队列可以是一个 void * (您没有显示它的实际类型,即 new_queue() 调用返回什么?),因此将其嵌入到添加附加参数时的结构:

struct queue_state {
void *queue;
int index;
};

然后实例化一个结构体,并将指向它的指针传递给线程函数:

struct queue_state qsa = malloc(sizeof *qsa);
if(qsa != NULL)
{
qsa->queue = new_queue();
qsa->index = 4711; /* or whatever */
pthread_create(&st[a], NULL, sendPacket, qsa);
}

然后线程函数就可以使用struct声明来访问所有字段。当然,声明需要位于两个 C 文件都包含的共享 header (例如 queue.h)中。

关于c - 在c中将队列作为参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10674162/

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