Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 方法签名和调用不正确_C_Pthreads - Fatal编程技术网

C 方法签名和调用不正确

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

所以我有一个程序,应该有消费者线程和生产者线程

在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_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,这看起来比我想象的要好。A
T(*)[]
与A
T**
不一样,您的编译器应该已经警告过您了。请回答您的问题,并添加编译此代码时收到的编译器诊断消息(逐字!)。谢谢您的评论和解释,看来问题解决了
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);
}