Linux 进程与线程五

时间:2023-03-08 20:25:24
Linux 进程与线程五
pthread_self函数
pthread_t pthread_self(void);
一般会成功,返回当前线程的ID
注意:在子线程中执行exit()函数会退出整个进程,一般使用pthread_exit函数
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <pthread.h> void *thread_run(void *arg)
{
int i=;
for(i=;i<;i++)
{
if(i==)
{
printf("线程1退出了!\n");
//注意:在子线程中执行exit()函数会退出整个进程,一般使用pthread_exit函数
exit();
}
printf("%d\n",i);
sleep();
}
return NULL;
} int main(int arg,char *args[])
{
pthread_t thr1;
if(pthread_create(&thr1,NULL,thread_run,NULL)!=)
{
printf("pthread_create() failed !\n");
return -;
}
//等待线程1
pthread_join(thr1,NULL);
printf("主线程完结1!\n");
return ;
}