//最末尾 填上需要链接pthread库的可执行文件
target_link_libraries(Ser_cmake pthread)
#include<pthread.h>
int main()
{
pthread_t pthid=0;
//创建一个线程
**if(pthread_create(&pthid,NULL,mainfunc,NULL)!=0)**
{
cout<<"create pthread failed"<<endl;
return -1;
}
cout<<"pthid= "<<pthid<<endl;
cout<<"waiting for child pthread"<<endl;
**pthread_join(pthid,NULL);**
cout<<"child ptread exited"<<endl;
return 0;
}
**void *mainfunc(void *arg) //注意是void *name (void *())
{
for(int i=0 ;i<30;i++)
{
cout<<"sleep 1sec "<<endl;
sleep(1);
}
};**
g++ -g -o m_exe main.cpp -lpthread 原本的linux库没有,需要自己加上
**pthread_create(&pthid,NULL,mainfunc,NULL)
1.第一个为线程标识符的地址
2.一般为空 NULL
3. 线程运行函数的地址 ,填函数名**
**pthread_join(pthid,NULL); 测试的使用**
如果任何一个线程调用的exit() 会导致所有的线程退出
return 用 pthread_exit()