pthread_join返回值
时间: 2023-09-15 14:21:13 浏览: 59
pthread_join函数返回的是线程的退出状态。如果线程已经退出,可以通过pthread_join获取该线程的退出状态。该函数的原型如下:
```c
int pthread_join(pthread_t thread, void **value_ptr);
```
其中,thread是要等待的线程的ID,value_ptr是一个指向指针的指针,用于存储线程的退出状态。
如果pthread_join函数成功调用,它将阻塞调用它的线程,直到指定的线程退出为止。在这之后,可以通过value_ptr获取到线程的退出状态。
返回值为0表示成功,非0表示失败,错误码可通过errno获取。
相关问题
pthread_create 返回值
pthread_create函数是用于创建一个新的线程的函数,它的返回值是一个整数类型的错误码。如果返回值为0,则表示线程创建成功;如果返回值为非零值,则表示线程创建失败,具体的错误码可以通过查阅相关文档来获取。在创建线程时,可以通过传递一个回调函数作为参数,该回调函数将在新线程中执行。回调函数的返回值类型为void指针,可以根据需要进行类型转换。在回调函数中,可以执行一些特定的操作,并返回一个值作为线程的返回值。在主线程中,可以使用pthread_join函数来等待新线程的结束,并获取其返回值。
以下是一个示例代码,演示了pthread_create函数的使用和回调函数的返回值获取[^1]:
```c
#include <stdio.h>
#include <pthread.h>
void *cb_fn(void *arg) //线程回调函数
{
printf("in cb_fn\n");
return (void *)"OK";
}
int main()
{
int ret;
void *retval;
pthread_t pid;
ret = pthread_create(&pid, NULL, cb_fn, NULL);
if (ret < 0) {
printf("pthread_create error, ret=%d\n", ret);
return -1;
}
pthread_join(pid, &retval);
printf("retval = %s\n", (char *)retval);
return 0;
}
```
在上述代码中,我们创建了一个新线程,并将cb_fn作为回调函数传递给pthread_create函数。在cb_fn函数中,我们打印了一条信息,并返回了一个字符串"OK"作为线程的返回值。在主线程中,我们使用pthread_join函数等待新线程的结束,并通过retval指针获取其返回值,并打印出来。
pthread_create和 pthread_join
pthread_create()和pthread_join()是Linux系统中用于创建和等待线程的函数。
pthread_create()函数用于创建一个新的线程,其原型如下:
```c
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
```
其中,thread参数是指向线程标识符的指针,attr参数是指向线程属性的指针,start_routine参数是指向线程函数的指针,arg参数是传递给线程函数的参数。
pthread_join()函数用于等待一个线程的结束,其原型如下:
```c
int pthread_join(pthread_t thread, void **retval);
```
其中,thread参数是要等待的线程标识符,retval参数是指向线程返回值的指针。
下面是一个简单的例子,演示了如何使用pthread_create()和pthread_join()函数创建和等待线程:
```c
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *print_message_function(void *ptr);
int main()
{
pthread_t thread1, thread2;
char *message1 = "Thread 1";
char *message2 = "Thread 2";
int ret1, ret2;
// 创建线程1
ret1 = pthread_create(&thread1, NULL, print_message_function, (void *)message1);
if (ret1)
{
printf("Error: pthread_create() failed\n");
exit(EXIT_FAILURE);
}
// 创建线程2
ret2 = pthread_create(&thread2, NULL, print_message_function, (void *)message2);
if (ret2)
{
printf("Error: pthread_create() failed\n");
exit(EXIT_FAILURE);
}
// 等待线程1结束
ret1 = pthread_join(thread1, NULL);
if (ret1)
{
printf("Error: pthread_join() failed\n");
exit(EXIT_FAILURE);
}
// 等待线程2结束
ret2 = pthread_join(thread2, NULL);
if (ret2)
{
printf("Error: pthread_join() failed\n");
exit(EXIT_FAILURE);
}
printf("Main thread exiting\n");
exit(EXIT_SUCCESS);
}
void *print_message_function(void *ptr)
{
char *message;
message = (char *)ptr;
printf("%s\n", message);
pthread_exit(NULL);
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)