gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:14:03 24 4
gpt4 key购买 nike

每当我尝试编译以下程序时,我都会从编译器 (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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com