【问题标题】:c pthread passing array of type intc pthread 传递 int 类型的数组
【发布时间】:2013-10-15 18:52:33
【问题描述】:

我正在传递一个 int pthread_create 类型的数组并收到错误:

  histogram.c:138:3: warning: passing argument 3 of
 ‘pthread_create’ from incompatible   pointer type [enabled by default]
  expected ‘void * (*)(void *)’ but argument is of type ‘void * (*)(int *)’

  void *output_results();
  pthread_create(&t2, NULL, output_results, (void *)bins);

  void *output_results(int *bins) {
      some code
  }

【问题讨论】:

    标签: c arrays pthreads


    【解决方案1】:

    编译错误是因为pthread_create 期望void *output_results(void *bins),但你有int *bins

    另外,您使用的output_results 的声明与其定义不匹配。

    【讨论】:

      【解决方案2】:

      应该是

      void *output_results(void*);
      pthread_create(&t2, NULL, output_results, (void *)bins);
      
      void *output_results(void *data) {
          int *bins = (int*)data;
          // some code
      }
      

      错误消息非常清楚:函数应该是void * (*)(void *) 类型而不是void * (*)(int *)(加上你的output_results 原型与它的定义不匹配)。

      【讨论】:

      • 没想到,为什么演员表在 pthread_create 中不起作用?
      • 您正在转换pthread_create 的第四个参数,而问题在于第三个参数:函数指针。 pthread_create 的原型要求第三个参数的类型为 void * (*)(void *)
      猜你喜欢
      • 1970-01-01
      • 2021-06-01
      • 2020-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多