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

时间:2021-02-19 21:00:37

I am writing a function to create thread and a function for waiting . but I got some error like

我正在编写一个函数来创建线程和一个等待的函数。但是我得到了一些错误

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

and

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

my code is here :

我的代码在这里:

main function :

主功能 :

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

in my function file :

在我的函数文件中:

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);

 }

Is it any declaration of pointer in correct ? why I can't return values from the thread function ? any problem in my code ?

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

1 个解决方案

#1


0  

Lets view the warnings one by one:

让我们逐一查看警告:

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

main.c :: warning:赋值来自整数而没有强制转换的指针

You are most probably getting this error here:

您最有可能在此处收到此错误:

thid = create_thread(argv,count);

The reason is that create_thread returns an argument of type pthread_t* while thid is of type pthread_t**.

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

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

function.c:在函数'create_thread'中:function.c :: warning:function返回局部变量的地址

The warning says it all. pthread_t thid[count]; is a local variable. So, return thid; returns the address of the first element of this array which will be invalid once the function create_thread exits.

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

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

function.c:在函数'wait_thread'中:function.c :: warning:传递'pthread_join'的参数1从指针生成整数而没有强制转换

The function pthread_join expects its first to be of type pthread_t. You give the argument thid[index] which is of type pthread_t*. This is what the compiler is warning about.

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

#1


0  

Lets view the warnings one by one:

让我们逐一查看警告:

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

main.c :: warning:赋值来自整数而没有强制转换的指针

You are most probably getting this error here:

您最有可能在此处收到此错误:

thid = create_thread(argv,count);

The reason is that create_thread returns an argument of type pthread_t* while thid is of type pthread_t**.

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

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

function.c:在函数'create_thread'中:function.c :: warning:function返回局部变量的地址

The warning says it all. pthread_t thid[count]; is a local variable. So, return thid; returns the address of the first element of this array which will be invalid once the function create_thread exits.

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

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

function.c:在函数'wait_thread'中:function.c :: warning:传递'pthread_join'的参数1从指针生成整数而没有强制转换

The function pthread_join expects its first to be of type pthread_t. You give the argument thid[index] which is of type pthread_t*. This is what the compiler is warning about.

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