gpt4 book ai didi

c - 为什么我不能在线程函数内返回值?

转载 作者:行者123 更新时间:2023-11-30 20:38:17 26 4
gpt4 key购买 nike

我正在编写一个创建线程的函数和一个等待的函数。但我遇到了一些错误,例如

main.c:: warning: assignment makes pointer from integer without a cast 

function.c: In function ‘create_thread’:
function.c:: warning: function returns address of local variable
function.c: In function ‘wait_thread’:
function.c:: warning: passing argument 1 of ‘pthread_join’ makes integer from pointer without a cast

我的代码在这里:

主要功能:

------------some declartions------
pthread_t **thid =NULL;
thid = create_thread(argv,count);
wait_thread(thid,count);
----------some code-----------------

在我的函数文件中:

pthread_t *  create_thread(char *argv[],
int count)
{
pthread_t thid[count];
-------some codes-------------
status = pthread_create(&thid[index],NULL,file_op,(void*)mystruct);
--------------------------
return thid;

}

void wait_thread(pthread_t **thid,int count)
{
------some codes-----------
ret = pthread_join(thid[index],&retval);

}

指针的声明是否正确?为什么我无法从线程函数返回值?我的代码有什么问题吗?

最佳答案

让我们一一查看警告:

main.c:: warning: assignment makes pointer from integer without a cast

您很可能在这里遇到此错误:

thid = create_thread(argv,count);

原因是 create_thread 返回类型为 pthread_t* 的参数,而 thid 类型为 pthread_t** >.

function.c: In function ‘create_thread’:
function.c:: warning: function returns address of local variable

警告说明了一切。 pthread_t thid[count]; 是一个局部变量。因此,return thid;返回该数组的第一个元素的地址,一旦create_thread函数退出,该地址将无效。

function.c: In function ‘wait_thread’:
function.c:: warning: passing argument 1 of ‘pthread_join’ makes integer from pointer without a cast

函数pthread_join期望它的第一个函数是pthread_t类型。您给出的参数 thid[index] 类型为 pthread_t*。这就是编译器警告的内容。

关于c - 为什么我不能在线程函数内返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29625906/

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