eg:pthread_create()
#include<stdio.h>
#include<pthread.h>
#include<unistd.h>
void *fun()
{
while(1){
//printf("pth:%d\n",pthread_self()); pthread_self()获取线程id
printf("pthread\n");
sleep)(1);
}
}
int main()
{
//创建线程
pthread_t pth1,pth2;
pthread_create(&pth1,NULL,(void *)fun,NULL);
pthread_create(&pth2,NULL,(void *)fun,NULL);
//pth1,pth2 线程id
//NULL 线程属性
//fun 函数指针
//NULL 函数参数
//编译 gcc xxx.c -lpthread
while(1){
printf("hello world\n");
sleep(1);
}
}



eg:pthread_exit(void *retval)
pthread_exit(void *retval) //线程退出

//在上图中,线程并发共用了堆空间。值得注意:子线程不可以把栈空间的变量返回给主线程。

网友评论