pthread_create,pthread_join和pthread_exit
时间: 2023-09-21 15:05:09 浏览: 73
pthread_create是一个函数,用于创建一个新的线程。它接受四个参数:指向pthread_t类型的指针thread,指向pthread_attr_t类型的指针attr,指向一个函数的指针start_routine和一个参数arg。start_routine是线程的入口函数,它接受一个void类型的指针参数并返回一个void类型的指针。pthread_create函数成功时返回0,失败时返回错误编号。
pthread_join是一个函数,用于等待一个线程的结束。它接受两个参数:pthread_t类型的参数thread和一个void类型的指针rval_ptr。rval_ptr用于获取线程的返回值。pthread_join函数会阻塞调用线程,直到指定的线程结束。成功时返回0,失败时返回错误编号。
pthread_exit是一个函数,用于终止当前线程。它接受一个void类型的指针参数rval_ptr,用于指定线程的返回值。调用pthread_exit函数会立即终止当前线程,并将指定的返回值传递给等待该线程的线程。它类似于进程的exit函数。pthread_exit函数不会返回任何值。
pthread_t *thread是一个指向pthread_t类型的指针,它用于存储线程的ID。可以使用pthread_self函数获取当前线程的ID,类似于使用getpid函数获取进程的ID。
综上所述,pthread_create用于创建新线程,pthread_join用于等待线程的结束,pthread_exit用于终止当前线程。
相关问题
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);
}
```
pthread_exit pthread_join 传递整型数据
引用\[1\]:在示例代码中,使用了pthread_create函数创建了一个新的线程,并在新线程中执行了new_thread_start函数。在主线程中,使用pthread_join函数等待新线程的结束,并获取新线程的返回值。最后,主线程打印出新线程的终止代码。\[1\]
引用\[2\]:在另一个示例代码中,同样使用了pthread_create函数创建了一个新的线程,并在新线程中执行了new_thread_start函数。不同的是,主线程没有使用pthread_join函数等待新线程的结束,而是直接退出。\[2\]
引用\[3\]:pthread_join函数用于等待指定的线程结束,并获取线程的返回值。它的第一个参数是需要等待的线程的ID号,第二个参数是一个指向指针的指针,用于存储线程的返回值。如果不关心线程的返回值,可以将第二个参数设置为NULL。pthread_exit函数用于退出当前线程,并返回一个指定的值。\[3\]
综上所述,pthread_exit函数用于退出线程,而pthread_join函数用于等待线程的结束并获取线程的返回值。如果需要传递整型数据,可以将整型数据作为参数传递给新线程的函数,并在新线程中进行相应的处理。
#### 引用[.reference_title]
- *1* *2* [Linux线程(2)——创建、终止和回收(pthread_create()、pthread_exit()、pthread_join())](https://blog.csdn.net/cj_lsk/article/details/130229709)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item]
- *3* [系统编程05-线程(pthread_create、pthread_join、pthread_exit)](https://blog.csdn.net/weixin_48102054/article/details/127331663)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](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)