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

来源:互联网 发布:mac怎么打拼音 编辑:程序博客网 时间:2024/05/16 09:09
#include "apue.h"/**1.main函数的线程称为初始线程或主线程,主线程在main函数返回的时候,会导致整个线程结束。可以在主线程中使用pthread_exit函数 退出主线程 如此,进程会等待所有的线程结束时候才终止*/struct person{int age;char name[10];};void *thread_fun(void *person1){//打印出当前线程的IDprintf("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}

0 0
原创粉丝点击