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=0;
for(i=0;i<10;i++)
{
if(i==5)
{
printf("线程1退出了!
");
//注意:在子线程中执行exit()函数会退出整个进程,一般使用pthread_exit函数
exit(0);
}
printf("%d
",i);
sleep(1);
}
return NULL;
}
int main(int arg,char *args[])
{
pthread_t thr1;
if(pthread_create(&thr1,NULL,thread_run,NULL)!=0)
{
printf("pthread_create() failed !
");
return -1;
}
//等待线程1
pthread_join(thr1,NULL);
printf("主线程完结1!
");
return 0;
}