Qt中遇到函数退出或线程退出时,进程却无法退出

Qt中写程序时,要到main函数中调用一个函数,或者一个线程退出时,整个进程却没有退出的情形:

int foo()
{
    return 0;
}

void* myPhread(void*)
{
    //do sth.
    pthread_exit(0);
}

int main(int argc,char* argv[])
{
    QApplication a(argc,argv);
    
    foo();
   
    // pthread_t tid;
    //pthread_create(&tid,NULL,myPthread,NULL);


    return a.exec();
}

上述程序中,无论是调用函数foo 的return; 还是线程 myPthread的 pthread_exit 后,整个进程依然没有退出,

问题原因:Qt中 a.exec()一直处于事件循环中,是不会退出程序的,所以, 需要在 程序中添加 退出进程函数exit(0);

int foo()
{
    return 0;
}

void* myPhread(void*)
{
    //do sth.
    pthread_exit(0);
}

int main(int argc,char* argv[])
{
    QApplication a(argc,argv);
    
    foo();
   
    // pthread_t tid;
    //pthread_create(&tid,NULL,myPthread,NULL);

    exit(0);//添加退出进程函数

    //return a.exec();
}

错误原因:以为线程退出或者函数返回,主进程就会退出,潜意识认为是main函数是 return 0;

但Qt 函数的特殊之后,就在函数一直处于事件循环中,而不退出程序。 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值