【问题标题】:Simple explanation of passing a structure to a thread将结构传递给线程的简单解释
【发布时间】:2014-05-05 18:15:57
【问题描述】:

不知道为什么,但是......什么会导致错误

ma​​in.c:126:4:错误:无法转换为指针类型 main.c:126:4:警告:从不兼容的指针类型传递“pthread_create”的参数 3 [默认启用] /usr/include/pthread.h:225:12:注意:预期为 'void * (*)(void )' 但参数的类型为 'void ()(struct arrayslice)'

据我所知,我已经正确地设计了这个函数的原型。

struct People{

    int count;
    int levels;
};



struct arrayslice *args = &current;
pthread_create(&thread, NULL, countall, (void*) &args);

【问题讨论】:

  • 这里有问题,您没有给我们足够的信息。 “pthread_create”的原型是什么样的?您正在向它传递错误的论点。我怀疑你正在传递一个双指针,而你应该传递一个。
  • 第三个参数是countall,但是你没有包含它的声明,那么你怎么能指望任何人回答你的问题呢?
  • 来吧,@RandomGuy,你可以在手册页上找到 pthread_create 的原型,并且除了错误消息给出了预期的类型。
  • 我有点懒,但我怀疑这应该是 pthread_create(&thread, NULL, countall, (void*) args);

标签: c error-handling pthreads


【解决方案1】:

您可以参考以下文章。这一篇确实包含有关 pthread 的有用信息以及相关示例和说明。

https://computing.llnl.gov/tutorials/pthreads/

预期为“void * (*)(void )”,但参数类型为“void ()(struct 数组切片)'

关于你的编译错误,这是因为你没有在第三个参数中传递正确的函数指针。看起来你的功能是

void countall(struct arrayslice);

但是 pthread 期望你的函数应该是

void* countall(void* arrayslice);

【讨论】:

猜你喜欢
  • 2021-06-30
  • 2017-04-14
  • 1970-01-01
  • 2018-04-17
  • 1970-01-01
  • 2021-10-07
  • 2019-11-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多