C++关于线程分离状态的编程(下)

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

一 获取线程的分离状态属性

1 代码

#ifndef _GNU_SOURCE  
#define _GNU_SOURCE     /* To get pthread_getattr_np() declaration */  
#endif  
#include <pthread.h>  
#include <stdio.h>  
#include <stdlib.h>  
#include <unistd.h>  
#include <errno.h>  
// 输出自定义的错误信息
#define handle_error_en(en, msg) \  
        do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)  
      

static void * thread_start(void *arg)  
{  
    int i,s;  
    pthread_attr_t gattr;  //定义线程属性结构体
    // 获取当前线程属性结构值  
    s = pthread_getattr_np(pthread_self(), &gattr);  
    if (s != 0)  
        handle_error_en(s, "pthread_getattr_np");  
      
    printf("Thread's detachstate attributes:\n");
    // 从属性结构值中获取分离状态属性
    s = pthread_attr_getdetachstate(&gattr, &i);  
    if (s)  
        handle_error_en(s, "pthread_attr_getdetachstate");     // 打印错误信息
    // 打印当前分离状态属性
    printf("Detach state        = %s\n",
        (i == PTHREAD_CREATE_DETACHED) ? "PTHREAD_CREATE_DETACHED" :  
        (i == PTHREAD_CREATE_JOINABLE) ? "PTHREAD_CREATE_JOINABLE" :  
        "???");  

     pthread_attr_destroy(&gattr);  
}  
      
int main(int argc, char *argv[])  
{  
    pthread_t thr;  
    int s;  
    s = pthread_create(&thr, NULL, &thread_start, NULL);  // 创建线程
    if (s != 0)  
    {
        handle_error_en(s, "pthread_create");
        return 0;
    }
    
    pthread_join(thr, NULL); //等待子线程结束
}  

2 运行

[root@localhost test]# g++ -o test test.cpp -lpthread
[root@localhost test]# ./test
Thread's detachstate attributes:
Detach state        = PTHREAD_CREATE_JOINABLE

3 说明

从运行结果来看,默认创建的线程就是一个可连接的线程,即其分离状态是可连接的。

二 把可连接线程转换为可分离线程

1 代码

#ifndef _GNU_SOURCE  
#define _GNU_SOURCE     /* To get pthread_getattr_np() declaration */  
#endif  
#include <pthread.h>  
#include <stdio.h>  
#include <stdlib.h>  
#include <unistd.h>  
#include <errno.h>  
     
static void * thread_start(void *arg)  
{  
    int i,s;  
    pthread_attr_t gattr;  
    s = pthread_getattr_np(pthread_self(), &gattr);  
    if (s != 0)  
        printf("pthread_getattr_np failed\n");  
    
    s = pthread_attr_getdetachstate(&gattr, &i);  
    if (s)  
        printf(  "pthread_attr_getdetachstate failed");  
    printf("Detach state        = %s\n",
        (i == PTHREAD_CREATE_DETACHED) ? "PTHREAD_CREATE_DETACHED" :  
        (i == PTHREAD_CREATE_JOINABLE) ? "PTHREAD_CREATE_JOINABLE" :  
        "???");  

    pthread_detach(pthread_self()); //转换线程为可分离线程
    
    s = pthread_getattr_np(pthread_self(), &gattr);  
    if (s != 0)  
        printf("pthread_getattr_np failed\n");  
    s = pthread_attr_getdetachstate(&gattr, &i);  
    if (s)  
        printf(" pthread_attr_getdetachstate failed");  
    printf("after pthread_detach,\nDetach state        = %s\n",
        (i == PTHREAD_CREATE_DETACHED) ? "PTHREAD_CREATE_DETACHED" :  
        (i == PTHREAD_CREATE_JOINABLE) ? "PTHREAD_CREATE_JOINABLE" :  
        "???");  
    
     pthread_attr_destroy(&gattr);  //销毁属性
}  
      
int main(int argc, char *argv[])  
{  
    pthread_t thread_id;  
    int s;  
    s = pthread_create(&thread_id, NULL, &thread_start, NULL);  
    if (s != 0)  
    {
        printf("pthread_create failed\n");
        return 0;
    }
    pthread_exit(NULL);//主线程退出,但进程并不马上结束
}  

2 运行

[root@localhost test]# g++ -o test test.cpp -lpthread
[root@localhost test]# ./test
Detach state        = PTHREAD_CREATE_JOINABLE
after pthread_detach,
Detach state        = PTHREAD_CREATE_DETACHED

猜你喜欢

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