您的位置:首页 > 运维架构 > Linux

Linux--多线程之线程的创建和退出

2016-12-03 19:39 585 查看
#include "apue.h"
/**
1.main函数的线程称为初始线程或主线程,主线程在main函数返回的时候,会导致
整个进程结束。可以在主线程中使用pthread_exit函数 退出主线程 如此,
进程会等待所有的线程结束时候才终止
*/
struct person{
int age;
char name[10];
};

void *thread_fun(void *person1){
//打印出当前线程的ID
printf("fun thread id=%lu\n", pthread_self());
printf("age =%d name=%s \n",((struct person*)person1)->age,((struct person*)person1)->name);
return NULL;
}

int main(){
pthread_t tid;
int err;
struct person per;
per.age = 20;
strcpy(per.name,"liu pan");

//创建线程
err = pthread_create(&tid,NULL,thread_fun,&per);
if(err!=0){
perror(" fail to create thread ");
return -1;
}

printf("success to create thread tid = %lu \n ",tid);
//打印出当前线程的ID
printf("main thread id=%lu\n", pthread_self());

//主线程退出
pthread_exit(NULL);//always succeeds
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  linux c语言
相关文章推荐