#include<stdio.h>#include<pthread.h>#include<stdlib.h>#include<unistd.h>void*thrd_func(void*arg){
printf("子进程ID:%u;子线程ID:%lu\n",getpid(),pthread_self());}intmain(){
pthread_t tid;int ret;printf("主进程ID:%u;主线程ID:%lu\n",getpid(),pthread_self());
ret =pthread_create(&tid,NULL,thrd_func,NULL);if(ret !=0){
printf("pthread_create error\n");exit(1);}sleep(1);return0;}
7.线程与共享
8.pthread_exit线程退出函数
#include<stdio.h>#include<stdlib.h>#include<unistd.h>#include<pthread.h>void*tfn(void*arg){
int i;
i =(int)arg;printf("i am %dth thread,thread_id:%lu\n",i+1,pthread_self());returnNULL;}intmain(){
int n=5,i;
pthread_t tid;for(i=0;i<n;i++){
pthread_create(&tid,NULL,tfn,(void*)i);}printf("i am main thread,thread id:%lu\n",pthread_self());pthread_exit(NULL);}