主线程退出后,子线程会不会退出

        额,好吧,这是个标题党,其实所有的线程都是平级的,根本不存在主线程和子线程。下文所述为了方便,将在main函数中的线程看做主线程,其它线程看成子线程,特此说明。先考虑以下代码:

        

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>

void* thrd_start_routine(void* v)
{
    sleep(10);
        printf("created thread\n");
}

int main()
{
        pthread_t thrdid;
                
        pthread_create(&thrdid, NULL, &thrd_start_routine, NULL);
        
        sleep(5);
        printf("main thread\n");        
        return  0;
}

运行,结果是5s休眠后,打印“main thread";程序退出

之前误认为线程和进程一样,主线程退出,子线程退出。再考虑以下代码;

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>

void* thrd_start_routine(void* v)
{
    sleep(10);
        printf("created thread\n");
}

int main()
{
        pthread_t thrdid;
                
        pthread_create(&thrdid, NULL, &thrd_start_routine, NULL);
        
        sleep(5);
        printf("main thread\n");
        pthread_exit(NULL);  
        printf("main exit\n");      
        return  0;
}

结果输出 main thread ;5s后输出 created thread;注意u,此处的main exit并不输出

相较于上一个程序,只加了一句 pthread_exit(NULL);

原因:

之前一个程序是在打印主线程之后,程序return,间接调用了exit()函数,因为一个线程调用exit函数,导致整个进程的退出,系统回收所有的资源,当然所有 的线程都退出了。再第二个程序中;找到以下资料:

When you program with POSIX Threads API, there is one thing about pthread_exit() that you may ignore for mistake. In subroutines that complete normally, there is nothing special you have to do unless you want to pass a return code back using pthread_exit(). The completion won't affect the other threads which were created by the main thread of this subroutine. However, in main(), when the code has been executed to the end, there could leave a choice for you. If you want to kill all the threads that main() created before, you can dispense with calling any functions. But if you want to keep the process and all the other threads except for the main thread alive after the exit of main(), then you can call pthread_exit() to realize it. And any files opened inside the main thread will remain open after its termination.

在主线程退出时,要想系统并不回收进程的所有资源,可以调用pthread_exit();然后等其他线程终止退出。


然后考察一到面试题:

是否熟悉POSIX多线程编程技术?如熟悉,编写程序完成如下功能:

1)有一int型全局变量g_Flag初始值为0;

2) 在主线称中起动线程1,打印“this is thread1”,并将g_Flag设置为1

3) 在主线称中启动线程2,打印“this is thread2”,并将g_Flag设置为2

4) 线程序1需要在线程2退出后才能退出

5) 主线程在检测到g_Flag从1变为2,或者从2变为1的时候退出


我的代码如下:

#include <iostream>
#include <pthread.h>
#include <stdlib.h>
using namespace std;

int flag=0;

pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond=PTHREAD_COND_INITIALIZER;

void* thread1(void *var);
void* thread2(void *var);

int main()
{
    pthread_t tid1,tid2;
    pthread_create(&tid1,NULL,thread1,&tid2);
    pthread_create(&tid2,NULL,thread2,NULL);
	pthread_cond_wait(&cond,&mutex);
	cout<<"init thread exit"<<endl;
	pthread_exit(NULL);
}

void* thread1(void *var)
{
//	pthread_detach(pthread_self());
	pthread_mutex_lock(&mutex);
	if(flag==2)
        pthread_cond_signal(&cond);
    cout<<"this is thread1"<<endl;
	flag=1;
	pthread_mutex_unlock(&mutex);
	pthread_join(*(pthread_t *)var,NULL);
    cout<<"thread1 exit"<<endl;
}

void* thread2(void *var)
{
//	pthread_detach(pthread_self());
	pthread_mutex_lock(&mutex);
	if(flag==1)
        pthread_cond_signal(&cond);
    cout<<"this is thread2"<<endl;
	flag=2;
	pthread_mutex_unlock(&mutex);
    cout<<"thread2 exit"<<endl;
}


如代码所示,如果此处在main函数中没有pthread_exit(),那在flag由1变2或者由2变1时,其它线程通知main函数中的线程,将使得主进程直接退出,其它另外的线程就终止了,可能等不到打印线程exit语句。

©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值