C 方法签名和调用不正确
所以我有一个程序,应该有消费者线程和生产者线程 在main方法中,我想调用C 方法签名和调用不正确,c,pthreads,C,Pthreads,所以我有一个程序,应该有消费者线程和生产者线程 在main方法中,我想调用init\u consumers(),它将调用ptread\u init() 下面是一些代码: int init_consumers(char *quantity, pthread_t **cons, void *complex_obj) { //seting the limit acording to "quantity" for (; i < limit; i++) pthread
init\u consumers()
,它将调用ptread\u init()
下面是一些代码:
int init_consumers(char *quantity, pthread_t **cons, void *complex_obj)
{
//seting the limit acording to "quantity"
for (; i < limit; i++)
pthread_create(cons[i], NULL, &consumer, &complex_obj);
return(i);
}
argv[2]
和argv[3]
是用户想要的生产者/消费者数量
另外,线程签名是:void*producer(void*args)代码>
我仍然会遇到*
和&
的问题,所以我的问题是在进行方法调用和签名时
我犯的错误是
n_prod_n_cons.c:158:6: note: expected ‘pthread_t * {aka long unsigned int *}’ but argument is of type ‘pthread_t (*)[50] {aka long unsigned int (*)[50]}’ int init_producers(char *quantity, pthread_t *prod, void *complex_obj)
如我的中所述(简洁),在main()
中有pthread\t
数组;将它们传递给启动器,就像传递int
数组一样。使启动器函数的签名与传递int
数组时所做的匹配。然后使用&array[index]
将指向数组中一行的指针传递给pthread\u create()
。这意味着:
在main()
中:
(您的代码正在向启动程序函数传递一个pthread\u t(*)[MAX\u PROD]
-指向固定大小数组pthread\u t
-的指针,这与预期为pthread\u t**
的签名大不相同)
发射器看起来像:
int init_consumers(char *quantity, pthread_t *cons, void *complex_obj)
{
// setting the limit acording to "quantity"
for (; i < limit; i++)
pthread_create(&cons[i], NULL, consumer, &complex_obj);
return(i);
}
int init_使用者(字符*数量、pthread_t*cons、void*复杂对象)
{
//根据“数量”设置限额
对于(;i
cons[i]
是pthread\u t
数组中的第i
项;传递它的地址给pthread\u create()
函数它期望的pthread\u t*
。调用init\u生产者(argv[2],prod,co)
。定义int init_producer(char*qty,pthread_t*cons,void*co)
并调用pthread_create(&cons[i],NULL,producer,co)
@JonathanLeffler,这看起来比我想象的要好。AT(*)[]
与AT**
不一样,您的编译器应该已经警告过您了。请回答您的问题,并添加编译此代码时收到的编译器诊断消息(逐字!)。谢谢您的评论和解释,看来问题解决了pthread_t*cons
是关键
init_producers(argv[2], prod, co);
init_consumers(argv[3], cons, co);
int init_consumers(char *quantity, pthread_t *cons, void *complex_obj)
{
// setting the limit acording to "quantity"
for (; i < limit; i++)
pthread_create(&cons[i], NULL, consumer, &complex_obj);
return(i);
}