#include <stdio.h>
#include <pthread.h>
//pthread_create创建线程
void* myfunc(void *arg)
{
printf("child pthread id:%ld\n",pthread_self());
return 0;
}
int main()
{
pthread_t pthid;
pthread_create(&pthid,NULL,myfunc,NULL);
printf("father pthread id:%ld\n",pthread_self());
for(int i = 0;i < 5;i++)
{
printf("i = %d\n",i);
}
sleep(2);
return 0;
}
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <stdlib.h>
//pthread_exit线程退出
void* myfunc(void *arg)
{
printf("child pthread id:%ld\n",pthread_self());
for(int i=0;i<5;i++)
{
printf("child pthread %d\n",i);
if(i == 2)
{
//exit child pthread
//exit(0);
//pthread_exit(NULL);
Int num = 100;
pthread_exit(&num);
}
}
return 0;
}
int main()
{
pthread_t pthid;
int ret;
ret = pthread_create(&pthid,NULL,myfunc,NULL);
if(ret != 0)
{
printf("error number is %d\n",ret);
printf("%s\n",strerror(ret));
}
printf("father pthread id:%ld\n",pthread_self());
int i = 0;
while(1)
{
i++;
printf("father pthread %d\n",i);
}
sleep(2);
return 0;
}
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <stdlib.h>
//pthread_join回收子线程资源
int num = 100;
void* myfunc(void *arg)
{
printf("child pthread id:%ld\n",pthread_self());
for(int i=0;i<5;i++)
{
printf("child pthread %d\n",i);
if(i == 2)
{
//exit child pthread
//exit(0);
//pthread_exit(NULL);
pthread_exit(&num);
}
}
return 0;
}
int main()
{
pthread_t pthid;
int ret;
ret = pthread_create(&pthid,NULL,myfunc,NULL);
if(ret != 0)
{
printf("error number is %d\n",ret);
printf("%s\n",strerror(ret));
}
printf("father pthread id:%ld\n",pthread_self());
void* ptr = NULL;
pthread_join(pthid,&ptr);//先将子线程回收
printf("num = %d\n",*(int *)ptr);
int i = 0;
while(i<10)
{
i++;
printf("father pthread %d\n",i);
}
sleep(2);
return 0;
}