简体   繁体   English

将函数指针传递给pthread_create(C)

[英]Pass function pointer in parameter to pthread_create, (C)

Here is a minimal example which illustrates my problem 这是一个说明我问题的最小示例

test.c: 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;
}

Vim/gcc output : 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 (*)()’

What * / & do I need to add/remove to pass foo to distribute which then passes it to a thread? 什么* / &我需要添加/删除以传递foo进行distribute ,然后再将其传递给线程?

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

void distribute ( void * (*f)(void *) ) {
  /* code */
}

should do the trick 应该做到的

because the prototype is: 因为原型是:

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));

The minimum recommended changes are: 建议的最小更改为:

void *foo(void *unused)
{
    printf("BlahBlahBlah\n");
    return 0;
}

void distribute(void *(*f)(void *))
{
    ...as before...
}

The pthread_create() function wants a pointer to a function that takes a void * argument and returns a void * result (though you haven't got to that error yet). pthread_create()函数需要一个指向带有void *参数并返回void *结果的函数的指针(尽管您尚未发现该错误)。 So, pass it a pointer to that type of function by making foo() into a function that takes a void * argument and returns a void * result. 因此,通过使foo()成为接受void *参数并返回void *结果的函数,将其传递给该函数类型的指针。 And, for what it's worth, you can almost certainly make foo() into a static function since it is unlikely you will call it directly from outside this file. 而且,就其价值而言,几乎可以肯定地使foo()成为静态函数,因为不太可能直接从此文件外部调用它。

This page seems to explain it quite well: http://publib.boulder.ibm.com/infocenter/iseries/v5r3/index.jsp?topic=%2Fapis%2Fusers_14.htm ; 该页面似乎对它进行了很好的解释: http : //publib.boulder.ibm.com/infocenter/iseries/v5r3/index.jsp?topic =% 2Fapis%2Fusers_14.htm ;

Often IBM documentation is very good, keep an eye on those ibm links when they show up ;). 通常,IBM文档非常好,当它们出现时,请注意这些ibm链接;)。

So, apparently you need a function pointer taking a void pointer in its argument. 因此,显然您需要一个函数指针,该函数指针的参数中应带有void指针。 Try 尝试

void distribute ( void *( *f )(void *) ) {...}

You will probably also need to change your definition of foo though. 但是,您可能还需要更改foo的定义。 See the following tutorial for function pointers: http://www.cprogramming.com/tutorial/function-pointers.html . 有关功能指针,请参见以下教程: http : //www.cprogramming.com/tutorial/function-pointers.html Note: I haven't tested it myself, so no guarantees whether it will work - but I hope it at least points you in the right directions ;). 注意:我自己尚未测试过,因此无法保证它是否会工作-但我希望它至少可以为您指明正确的方向;)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM