gpt4 book ai didi

c - 为什么函数返回类型需要是 (void*) 类型才能在 C 中进行线程化?

转载 作者:太空狗 更新时间:2023-10-29 15:59:18 26 4
gpt4 key购买 nike

在c中,我们像这样创建一个线程:

void * run(void * arg){
printf("hello world\n");
}

int main(){
pthread_t thread;
int a = pthread_create(&thread, NULL, run, (void*)0);
}

但是如果我声明run as 就不行了

void run(){}

另一方面,如果我在 pthread_create 的参数中将它转换为 (void *),它工作正常。所以它只接受返回类型为 (void *) 的函数。

为什么?

谢谢!

最佳答案

线程函数必须声明为返回void *,因为线程库需要这样的返回值,并且会在之后将其存储到给定给pthread_join()的位置线程终止。

如果您不需要任何线程返回值,您可以只return 0;

关于c - 为什么函数返回类型需要是 (void*) 类型才能在 C 中进行线程化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10457359/

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