为什么编译器明明没有错误却在这里抱怨?
每当我尝试编译以下程序时,我都会从编译器(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);
}
Whenever I try to compile the following program, I get this message from the compiler (g++ 4.4.3). Any idea, why?
main.cpp: In function ‘int main(int, char**)’:
main.cpp:52: error: void value not ignored as it ought to be
Line 52 has the code 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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
pthread_create_with_stack
返回void
,但您试图将此void
“值”保存在int
中,即一个错误。pthread_create_with_stack
returnsvoid
, yet you're trying to save thisvoid
"value" in anint
, which is an error.就是这一行
您对 pthread_create_with_stack 的定义是 void 类型。应该是 void* 类型并返回 rc,即 pthread_create() 的结果。
由于 pthread_create_with_stack 是一个 void 函数,并且它在定义中不返回任何内容,因此将 rc 设置为其返回值不仅没有意义,而且是一个错误 gcc/g++ 甚至不会让您尝试编译。
It's this line
Your definition of pthread_create_with_stack is of type void. Should be of type void* and return rc, the result of pthread_create().
Since pthread_create_with_stack is a void function, and it returns nothing in the definition, setting rc to its return value is not only meaningless, it's an error gcc/g++ won't even let you try to compile.
是的,有一个错误。返回类型
void
表示该函数不返回任何值。您尝试将pthread_create_with_stack
的返回值分配给局部变量,但没有可分配的返回值。您应该将
pthread_create_with-stack
声明为返回int
,然后使其实际返回一个值:Yes, there is an error. A return type of
void
means that the function returns no value. You're trying to assign the return value ofpthread_create_with_stack
to a local variable, but there is no return value to assign.You should instead declare
pthread_create_with-stack
as returning anint
, and then make it actually return a value:您正在从
void
函数获取返回值并尝试将其分配给变量。pthread_create_with_stack
不返回任何内容;不要分配它或使用它。You're taking the return value from a
void
function and trying to assign it to a variable.pthread_create_with_stack
doesn't return anything; don't assign it or use it.您的
pthread_create_with_stack
是一个void
函数。它不返回值。您不能将其结果分配给变量。Your
pthread_create_with_stack
is avoid
function. It doesn't return a value. You can't assign it's result to a variable.pthread_create_with_stack 被定义为返回 void。
其中 rc 定义为 int 是不合法的
pthread_create_with_stack is defined as returning void.
where rc is defined as int is not legal
void 只是意味着“什么都没有”
你根本无法保存“什么”!
void simply means "nothing"
N you simply cant save "nothing"!