将参数中的函数指针传递给pthread_create,(C)

puk*_*puk 0 c multithreading gcc pthreads

这是一个说明我问题的最小例子

test.c的:

#include <stdio.h>
#include <pthread.h>

#define CORES 8

pthread_t threads [ CORES ];
int threadRet [ CORES ];

void foo ()
{
   printf ("BlahBlahBlah\n" );
}

void distribute ( void ( *f )() )
{
   int i;

   for ( i = 0; i < CORES; i++ )
   {
      threadRet [ i ] = pthread_create ( &threads [ i ], NULL, f, NULL );
   }
   for ( i = 0; i < CORES; i++ )
   {
      pthread_join ( threads [ i ], NULL );
   }
}

int main ()
{
   distribute ( &foo );
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

Vim/gcc输出:

test.c:20|11| warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type [enabled by default]
/usr/include/pthread.h:225|12| note: expected ‘void * (*)(void *)’ but argument is of type ‘void (*)()’
Run Code Online (Sandbox Code Playgroud)

什么*/ &我需要添加/删除传递foodistribute然后传递给线程?

Ale*_*lex 5

void *foo (void *x)
{
   printf ("BlahBlahBlah\n" );
}

void distribute ( void * (*f)(void *) ) {
  /* code */
}
Run Code Online (Sandbox Code Playgroud)

应该做的伎俩

因为原型是:

extern int pthread_create (pthread_t *__restrict __newthread,
                           __const pthread_attr_t *__restrict __attr,
                           void *(*__start_routine) (void *),
                           void *__restrict __arg) __THROW __nonnull ((1, 3));
Run Code Online (Sandbox Code Playgroud)