C++关于线程结束编程

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chengqiuming/article/details/89070942

一 线程主动结束

1 点睛

线程主动结束一般是线程函数使用return语句或调用pthread_exit函数。

2 线程终止并得到线程的退出码

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

#define PTHREAD_NUM    2

void *thrfunc1(void *arg)   // 第一个线程函数
{
    static int count = 1;   // 这里需要的是静态变量
    // pthread_exit 函数的参数类型是(void *),因此只能通过指针的形式出去,需要把整型变量count转换为整型指针,即&count,它是int *类型,
    // 这个时候再与void * 匹配,需要进行强制类型的转换
    pthread_exit((void*)(&count));   // 调用pthread_exit结束线程
}
void *thrfunc2(void *arg)
{
    static int count = 2;
    return (void *)(&count);   // 线程函数返回
}

int main(int argc, char *argv[])
{
    pthread_t pid[PTHREAD_NUM];   //定义两个线程id
    int retPid;
    int *pRet1; //注意这里是指针
    int * pRet2;


    if ((retPid = pthread_create(&pid[0], NULL, thrfunc1, NULL)) != 0)  // 创建第1个线程
    {
        perror("create pid first failed");
        return -1;
    }
    if ((retPid = pthread_create(&pid[1], NULL, thrfunc2, NULL)) != 0)  // 创建第2个线程
    {
        perror("create pid second failed");
        return -1;
    }

    if (pid[0] != 0)
    {
        pthread_join(pid[0], (void**)&pRet1);   //
        printf("get thread 0 exitcode: %d\n", *pRet1);
    }
    if (pid[1] != 0)
    {
        pthread_join(pid[1], (void**)&pRet2);
        printf("get thread 1 exitcode: %d\n", *pRet2);
    }
    return 0;
}

3 结果

[root@localhost test]# g++ -o test test.cpp -lpthread
[root@localhost test]# ./test
get thread 0 exitcode: 1
get thread 1 exitcode: 2

二 线程被动结束

1 点睛

一个线程可能在执行一项耗时的计算任务,用户可能没耐心,希望结束该线程。此时线程就要被动地结束了。如何被动结束呢?一种是在同进程的另一个线程通过pthread_kill发送信号给要结束的线程,目标线程收到信号后再退出;另一种方法是在同进程的其他线程中通过函数pthread_cancel来取消目标线程的执行。

2 代码

#include <iostream>  
#include <pthread.h>  
#include <signal.h>  
#include <unistd.h> //sleep
using namespace std;

static void on_signal_term(int sig)  // 信号处理函数
{
    cout << "sub thread will exit" << endl;
    pthread_exit(NULL);
}
void *thfunc(void *arg)
{
    signal(SIGQUIT, on_signal_term);  // 注册信号处理函数
    int tm = 50;
    while (true)      // 死循环,模拟一个长时间计算任务
    {
        cout << "thrfunc--left:"<<tm<<" s--" <<endl;
        sleep(1);
        tm--;
    }
    
    return (void *)0;   
}

int main(int argc, char *argv[])
{
    pthread_t     pid;  
    int res;
    
    res = pthread_create(&pid, NULL, thfunc, NULL);     // 创建子线程
    
    sleep(5);  // 让出CPU 5秒,让子线程执行
    
    pthread_kill(pid, SIGQUIT);  // 5秒结束后,开始向子线程发送SIGQUIT信号,通知其结束
    pthread_join(pid, NULL); // 等待子线程结束
    cout << "sub thread has completed,main thread will exit\n";
     
    return 0;
}

3 结果

[root@localhost test]# g++ -o test test.cpp -lpthread
[root@localhost test]# ./test
thrfunc--left:50 s--
thrfunc--left:49 s--
thrfunc--left:48 s--
thrfunc--left:47 s--
thrfunc--left:46 s--
sub thread will exit
sub thread has completed,main thread will exit

4 说明

子线程在执行的时候,主线程等了5秒后就开始向其发送信号SIGQUIT。在子线程中已经注册了SIGQUIT的处理函数on_signal_term。如果不注册SIGQUIT的处理函数,就将调用默认处理,即结束线程所属的进程。

猜你喜欢

转载自blog.csdn.net/chengqiuming/article/details/89070942