c++ - 为什么编译器在显然没有错误的情况下在这里提示?

标签 c++ c linux gcc g++

每当我尝试编译以下程序时,我都会从编译器 (g++ 4.4.3) 收到此消息。任何想法,为什么?

main.cpp: In function ‘int main(int, char**)’:
main.cpp:52: error: void value not ignored as it ought to be

第 52 行有代码 rc = pthread_create_with_stack( &thread[t], BusyWork, t );

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define NUM_THREADS 4

void *stackAddr[NUM_THREADS];
pthread_t thread[NUM_THREADS];
pthread_attr_t attr;

void *BusyWork(void *t)
{
   int i;
   long tid;
   double result=0.0;
       tid = (long)t;
   printf("Thread %ld starting...\n",tid);
   for ( i = 0; i < 1000; i++)
   {
      result = result + sin(i*tid) * tan(i*tid);
   }
   printf("Thread %ld done. Result = %e\n", tid, result);
   pthread_exit((void*) t);
}

void pthread_create_with_stack( pthread_t * pthread, void *(*start_routine) (void *), int tid )
{
    const size_t STACKSIZE = 0xC00000; //12582912
    int rc;
    size_t i;
    pid_t pid;

    stackAddr[tid] = malloc(STACKSIZE);
    pthread_attr_setstack(&attr, stackAddr[tid], STACKSIZE);

    rc = pthread_create( pthread, &attr, start_routine, (void*)0 );
}

int main (int argc, char *argv[])
{
   int rc;
   long t;
   void *status;

   /* Initialize and set thread detached attribute */
   pthread_attr_init(&attr);
   pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

   for(t=0; t<NUM_THREADS; t++) 
   {
      printf("Main: creating thread %ld\n", t);
      // The following line is the line 52, where error occurs
      rc = pthread_create_with_stack( &thread[t], BusyWork, t ); 
      if (rc) 
      {
         printf("ERROR; return code from pthread_create() is %d\n", rc);
         exit(-1);
      }
   }

   /* Free attribute and wait for the other threads */
   pthread_attr_destroy(&attr);
   for(t=0; t<NUM_THREADS; t++) 
   {
      rc = pthread_join(thread[t], &status);
      if (rc) 
      {
         printf("ERROR; return code from pthread_join() is %d\n", rc);
         exit(-1);
      }
      printf("Main: completed join with thread %ld having a status"   
            "of %ld\n",t,(long)status);
    }

    printf("Main: program completed. Exiting.\n");
    pthread_exit(NULL);
}

最佳答案

pthread_create_with_stack 返回 void,但您正试图将此 void“值”保存在 int 中,这是一个错误。

关于c++ - 为什么编译器在显然没有错误的情况下在这里提示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7955459/

相关文章:

c++ - 关于使用新运算符的 C++ 范围

linux - 从 Linux-EFI 链式加载 Windows-EFI

regex - 在 linux : suppress part of string 中使用正则表达式重命名文件

c++ - 如何解决在 opencv 中显示空图像的问题?

c++ - C++处理文本文件——从文本文件中提取某个字符串

c - 为什么需要 int 类型来处理 EOF 和 getchar() 的返回?

c - 将指针传递给函数的最佳用例

linux - 在 ubuntu precise(12.04) 上安装 glibc 11

c++ - "stdio"和 "stdlib"在 C 中代表什么?

c - 文件 c 中的多个结构