9. Operating system - related attributes of threads (1) (thread separation and combination)

Table of contents

1. API of thread-related properties

1. Special properties of threads

2. Set the properties of the thread

3. Precautions

Second, the use of thread attribute variables

3. Related APIs

1. pthread_attr_init/pthread_attr_destroy (initialization and destruction of thread attribute variables)

2. pthread_attr_setdetachstate, pthread_attr_getdetachstate (get and set the separation attribute of the thread)

3. Make the zombie process reclaimed by the system and get the API of its exit value

(1) pthread_exit (exit thread)

(2) pthread_join, pthread_tryjoin_np (join the specified thread)

Fourth, combine the attribute code

5. Thread combination of separation attributes

1. API of thread-related properties

1. Special properties of threads

2. Set the properties of the thread

3. Precautions

  • The above APIs are all for thread attribute operations, and thread attributes are variables of type pthread_attr_t
  • When setting the properties of a thread, add the required properties to the type variable through the above function interface, and then use the second parameter of pthread_create( ) to create a thread with the corresponding properties
  • Setting the properties of the thread must be done before the creation is successful

Second, the use of thread attribute variables

a. Define the thread attribute variable and initialize it with pthread_attr_init( ).

b. Use pthread_attr_setXXX( ) to set related attributes.

c. Use the thread attribute variable to create a corresponding thread.

d. Use pthread_attr_destroy( ) to destroy the thread attribute variable.

3. Related APIs

1. pthread_attr_init/pthread_attr_destroy (initialization and destruction of thread attribute variables)

2. pthread_attr_setdetachstate, pthread_attr_getdetachstate (get and set the separation attribute of the thread)

Notice

(1) Threads are joined by default

(2) If the thread is joined, it means that the thread will not automatically release its own resources when it exits, and will become a zombie process, and the exit value of the thread can be obtained by other threads.

(3) If the exit value of a certain thread is not needed, set the thread to the detached state to ensure that the thread will not become a zombie process

3. Make the zombie process reclaimed by the system and get the API of its exit value

By default, after exiting, it will become a zombie process and keep the exit value

Other threads can make their resources reclaimed by the system through pthread_exit, and can also get the exit value

(1) pthread_exit (exit thread)

By default, after exiting, it will become a zombie process and keep the exit value

Other threads can make their resources reclaimed by the system through pthread_exit, and can also get the exit value

(2) pthread_join, pthread_tryjoin_np (join the specified thread)

Note:
(1) If there is no exit value when the thread exits, then retval can be specified as NULL.

(2) If the thread specified by pthread_join( ) is still running, it will block and wait.

(3) If the thread specified by pthread_tryjoin_np( ) is still running, it will immediately return an error.

Fourth, combine the attribute code

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

void * func (void * arg)
{
    while(1)
    {
        printf("这里是func线程,线程ID :%ld \n" , pthread_self() );
        sleep(3);
        break ;
    }

    int * p = calloc(1,4);
    *p = 1024444 ;
    // 退出本线程并设置返回值的地址(返回了num 的地址)
    pthread_exit((void *)p); //返回的内存地址应该时一个堆空间
}

int main(int argc, char const *argv[])
{
    
    // 创建线程
    pthread_t t_id  = -1 ;
        
    pthread_create( &t_id , //新线程ID号
                    NULL , // 线程属性, NULL 默认属性
                    func,  // 线程需要执行的例程(新线程需要执行的任务《函数》) 
                    NULL ); // 线程的参数

    printf("t_id : %ld\n" , t_id) ;

    printf("这里是主函数,线程ID :%ld \n" , pthread_self() );

    int * retval ;
    // 阻塞等待接合线程
    printf("等待 function 线程退出:\n");
    pthread_join( t_id , (void*)&retval);
    printf("结合线程成功, 退出值为:%d\n" , *retval);

    // 尝试接合线程  (非阻塞)
    // int ret_val = 0 ;
    // if( ret_val = pthread_tryjoin_np( t_id , (void*)&retval))
    // {
    //     fprintf(stderr , "接合失败:%s\n" , strerror(ret_val));
    // }

    return 0;
}

If the thread exits, the exit value is the value of *p

5. Thread combination of separation attributes

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

void * func (void * arg)
{
    while(1)
    {
        printf("这里是func线程,线程ID :%ld \n" , pthread_self() );
        sleep(3);
        break ;
    }

    int * p = calloc(1,4);
    *p = 1024444 ;
    // 退出本线程并设置返回值的地址(返回了num 的地址)
    pthread_exit((void *)p); //返回的内存地址应该时一个堆空间
}

int main(int argc, char const *argv[])
{

    // 创建线程属性变量
    pthread_attr_t attr ;
    // 初始化线程属性
    pthread_attr_init(&attr);
    // 设置属性信息 (分离属性)
    if(pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED))
    {
        printf("设置分离属性失败!!\n");
    }
    else{
        printf("设置分离属性成功!!\n");
    }

    
    // 创建线程
    pthread_t t_id  = -1 ;
        
    pthread_create( &t_id , //新线程ID号
                    &attr , // 线程属性(当前设置为分离)
                    func,  // 线程需要执行的例程(新线程需要执行的任务《函数》) 
                    NULL ); // 线程的参数

    printf("t_id : %ld\n" , t_id) ;

    printf("这里是主函数,线程ID :%ld \n" , pthread_self() );

    int * retval ;
    int ret_val = 0 ;
    // 阻塞等待接合线程
    printf("等待 function 线程退出:\n");
    if(ret_val = pthread_join( t_id , (void*)&retval))
    {
        fprintf(stderr , "接合失败:%s\n" , strerror(ret_val));
        return -1 ;
    }
    printf("结合线程成功, 退出值为:%d\n" , *retval);

    // 尝试接合线程  (非阻塞)
    // int ret_val = 0 ;
    // if( ret_val = pthread_tryjoin_np( t_id , (void*)&retval))
    // {
    //     fprintf(stderr , "接合失败:%s\n" , strerror(ret_val));
    // }

    return 0;
}

Output: Binding failed: Invalid atgument

It is set as a separate attribute and cannot be combined

Guess you like

Origin blog.csdn.net/weixin_45981798/article/details/129811381