将'void (void *) 传递给'void * 类型的参数的不兼容指针类型

incompatible pointer types passing 'void (void *) to parameter of type 'void *

我创建了 pthread 如下:

void function1(void *s) {
start = (*(int *)s ;
}

pthread_t threads[numthreads];
int ids[numthreads];
for (i = 0; i < numthreads; i++) {
    ids[i] = i;
    int * p = &ids[i] ;
    pthread_create(&threads[i], NULL, function1, (void *)p);
}

但这给我错误:

>> mpicc -o hprogram hprogram.c
warning: incompatible pointer types passing 'void (void *)' to
      parameter of type 'void * _Nullable (* _Nonnull)(void * _Nullable)'
      [-Wincompatible-pointer-types]
                        pthread_create(&threads[i], NULL, function1, (void *)...
                                                          ^~~~~~~~~~
/usr/include/pthread.h:328:31: note: passing argument to parameter here
                void * _Nullable (* _Nonnull)(void * _Nullable),
                                            ^
1 warning generated.


这是一个 mpi 程序,我正在使用 pthreads 创建一个混合 mpi。

pthread_create() 需要一个指向以 void* 作为输入和 returns a void* 作为输出的函数的指针,但是你的函数 returns a void 代替。你只需要在return类型中添加一个*,并添加一个return语句,eg:

void* function1(void *s) {
    start = *(int *)s;
    return NULL; // <-- or whatever you want
}