/********************************
* file name: class.c
* author : wvjnuhhail@sina.cn
* data : 2024/05/29
* function : 主线程中创建一个子线程,容纳后让主线程先退出并返回一个值,
* 子线程接合主线程后输出主线程的退出值,然后子线程退出。
* note : None
*
*
* CopyRight (c) 2023-2024 All Right Reseverd
*
* *********************************************************************************/
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
pthread_t pthreadMainId;
//线程
void *test(void *arg)
{
int *exit_status;
while (1)
{
pthread_join(pthreadMainId, (void **)&exit_status);
printf("main thread eixt status code = %d\n", *exit_status);
pthread_exit(NULL);
}
}
int main()
{
// 创建子线程
pthread_t thread;
pthread_create(&thread, NULL, test, NULL);
// 获取自身id
pthreadMainId = pthread_self();
// 状态值为10
int exit_status = 10;
sleep(3);
// 主线程比子线程先结束
pthread_exit((void *)&exit_status);
return 0;
}