pthread_create和pthread_join
时间: 2023-09-12 21:04:05 浏览: 52
pthread_create是一个函数,用于创建一个新的线程。它接受四个参数:线程标识符指针、线程属性、线程运行函数和可选的运行函数参数。线程标识符指针是一个输出参数,用于存储新线程的标识符。
pthread_join也是一个函数,用于等待一个指定的线程结束。它接受两个参数:被等待的线程标识符和一个输出参数,用于存储被等待线程的返回值。
在使用pthread_create创建线程后,新线程会开始执行与之关联的运行函数。而在主线程中,可以使用pthread_join函数来等待新线程的结束,并且获取它的返回值。调用pthread_join函数会使主线程阻塞,直到被等待的线程结束。
这两个函数一般是一起使用的,通过创建线程并等待线程结束,可以实现多线程并发执行的效果。
相关问题
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_create和pthread_join函数
pthread_create函数用于创建一个新的线程,它的原型为:
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg);
其中,thread是指向线程标识符的指针,attr是指向线程属性的指针,start_routine是线程函数的指针,arg是传递给线程函数的参数。
pthread_join函数用于等待一个线程的结束,它的原型为:
int pthread_join(pthread_t thread, void **retval);
其中,thread是要等待的线程标识符,retval是指向线程返回值的指针。
这两个函数都是 POSIX 线程库中的函数,用于多线程编程。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)