#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include<pthread.h>
//线程是个独立调度的执行流,他需要执行一个代码块
//与进程不同,进程是用的和父进程一样的代码块
//给线程定义一个代码块,线程创建后,自动执行此代码块---函数方式
void *t_func(void *arg)
{
printf(" I am thread.\n");
return((void *)0);
}
int main()
{
pthread_t tid;//线程编号,线程id
tid=pthread_create(&tid,NULL,t_func,NULL);
sleep(1);//休眠一会,让线程先有机会执行完
//创建线程,主线程(进程)不应立即退出,否则线程也会被终止,无法执行完
//因为线程受进程控制,与子进程不一样,父进程退出,不会导致子进程退出
//但会导致线程退出
printf("main thread here.\n");
return(0);//main函数中的return等价exit()系统调用,退出线程
}