✨主进程以pthread_exit(0)方式退出——线程退出
✨子线程pthread_cancel方式杀死父线程
✨主线程以return,exit(0)方式退出——进程退出
🔱return
🔱return在主函数
🔶return在子函数
🔶exit(0)
🔶exit(0)在主函数
🔱exit(0)在其他函数
✨主进程先于子进程退出状态时间线
✨总结
✨主进程以pthread_exit(0)方式退出——线程退出
主进程显示僵尸状态,子进程正常运行
void* Routine(void* t)
{
int cnt=10;
while(cnt--)
{
cout<<"thread_1 is running"<<endl;
sleep(1);
}
pthread_exit(0);
}
//1.主线程pthread_exit(0)退出对子线程的影响
void test1()
{
// 主线程return退出——子线程能不能正常执行
pthread_t tid;
pthread_create(&tid,nullptr,Routine,nullptr);
int cnt=5;
while(cnt--)
{
cout<<"main thread is running"<<endl;
sleep(1);
}
pthread_exit(0);
// exit(0);// exit(0)是进程终止
return;// 函数终止,线程并没有终止
// 主线程pthread_exit(0)退出,子线程正常执行
}
int main()
{
test1();
return 0;
}
✨子线程pthread_cancel方式杀死父线程
子线程可以pthread_cancel方式杀死父线程,但是线程不能自杀
void* Routine(void* t)
{
pthread_t id = *(pthread_t*)t;
int cnt=10;
while(cnt--)
{
cout<<"thread_1 is running"<<endl;
if(cnt==7) pthread_cancel(id);// 子线程在第3s的时候杀死主线程
sleep(1);
}
pthread_exit(0);
}
//1.主线程pthread_exit(0)退出对子线程的影响
void test1()
{
// 主线程return退出——子线程能不能正常执行
pthread_t id = pthread_self();
pthread_t tid;
pthread_create(&tid,nullptr,Routine,&id);
int cnt=5;
while(cnt--)
{
cout<<"main thread is running"<<endl;
sleep(1);
}
// pthread_exit(0);
// exit(0);// exit(0)是进程终止
return;// 函数终止,线程并没有终止
// 主线程pthread_exit(0)退出,子线程正常执行
}
int main()
{
test1();
return 0;
}
主线程以return,exit(0)方式退出——进程退出
进程退出会直接终结所有线程
return
return在主函数
主函数return是终止进程
void* Routine(void* t)
{
int cnt=10;
while(cnt--)
{
cout<<"thread_1 is running"<<endl;
sleep(1);
}
pthread_exit(0);
}
int main()
{
// test1();
// 主线程return退出对子线程的影响
pthread_t tid;
pthread_create(&tid,nullptr,Routine,nullptr);
int cnt=5;
while(cnt--)
{
cout<<"main thread is running"<<endl;
sleep(1);
}
// 主线程return退出,相当于exit,直接杀死进程
return 0;
}
return在子函数
子函数return只是终止函数,并不会终止进程
void* Routine(void* t)
{
int cnt=10;
while(cnt--)
{
cout<<"thread_1 is running"<<endl;
sleep(1);
}
pthread_exit(0);
}
//1.主线程pthread_exit(0)退出对子线程的影响
void test1()
{
// 主线程return退出——子线程能不能正常执行
pthread_t tid;
pthread_create(&tid,nullptr,Routine,nullptr);
int cnt=5;
while(cnt--)
{
cout<<"main thread is running"<<endl;
sleep(1);
}
// pthread_exit(0);
// exit(0);// exit(0)是进程终止
return;// 函数终止,线程并没有终止
// 主线程pthread_exit(0)退出,子线程正常执行
}
int main()
{
test1();
// 主线程return退出对子线程的影响
// pthread_t tid;
// pthread_create(&tid,nullptr,Routine,nullptr);
int cnt=5;
while(cnt--)
{
cout<<"main thread is running"<<endl;
sleep(1);
}
// 主线程return退出,相当于exit,直接杀死进程
// exit(0);
return 0;
}
exit(0)
exit不论在哪里都是终止进程
exit(0)在主函数
void* Routine(void* t)
{
int cnt=10;
while(cnt--)
{
cout<<"thread_1 is running"<<endl;
sleep(1);
}
pthread_exit(0);
}
int main()
{
// test1();
// 主线程return退出对子线程的影响
pthread_t tid;
pthread_create(&tid,nullptr,Routine,nullptr);
int cnt=5;
while(cnt--)
{
cout<<"main thread is running"<<endl;
sleep(1);
}
// 主线程return退出,相当于exit,直接杀死进程
exit(0);
return 0;
}
exit(0)在其他函数
void* Routine(void* t)
{
int cnt=10;
while(cnt--)
{
cout<<"thread_1 is running"<<endl;
sleep(1);
}
pthread_exit(0);
}
//1.主线程pthread_exit(0)退出对子线程的影响
void test1()
{
// 主线程return退出——子线程能不能正常执行
pthread_t tid;
pthread_create(&tid,nullptr,Routine,nullptr);
int cnt=5;
while(cnt--)
{
cout<<"main thread is running"<<endl;
sleep(1);
}
// pthread_exit(0);
exit(0);// exit(0)是进程终止
// return;// 函数终止,线程并没有终止
}
int main()
{
test1();
return 0;
}
✨主进程先于子进程退出状态时间线
不需要担心僵尸资源不会释放的问题,最终交由OS会释放
✨总结
- 主进程先于子进程退出——主进程僵尸,子进程正常运行
- exit——进程退出
- main函数return——进程退出
- 其他函数退出——函数退出
- pthread_exit(0)——线程退出
- pthread_cancel(id)——线程退出,不能自己杀自己,可以子杀父