- #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--多线程之线程的创建和退出
最新推荐文章于 2023-03-14 08:13:00 发布