当前位置:网站首页>5. Thread separation

5. Thread separation

2022-07-18 19:56:00 123_ YQH

Thread separation

1. summary

In the last section we knew , After the thread ends, it also needs to be recycled , Otherwise, it will generate Zombie Threads . Before using Connection recycling , Other threads are required to pass pthread_join The way to recycle .

In this section, we introduce a simpler way , Main thread pass pthread_detach() Function will Child threads are marked as detached . When a separate thread terminates , Its resources are automatically released back to the system , There is no need for another thread to join the terminating thread .

Try Connect already Separate Threads of will lead to unknown results .

2. function API

#include <pthread.h>
int pthread_detach(pthread_t thread);
-  function : Separate a thread . When the separated thread terminates , It will automatically release resources and return them to the system .
    1. Cannot separate multiple times , Will produce unpredictable behavior .
    2. Cannot connect to a thread that has been separated , Will report a mistake .
    -  Parameters : The number of threads that need to be separated ID
    -  Return value :
     success :0
     Failure : Return error number 

3. Case study —— The main thread marks the child threads as detached

Marking child threads as detached is simple , You only need to get its thread when you create a child thread ID, And then call pthread_detach( Sub thread ID) that will do . The code is as follows :

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

void * callback(void * arg) {
    
    printf("chid thread id : %ld\n", pthread_self());
    return NULL;
}

int main() {
    

    //  Create a child thread 
    pthread_t tid;

    int ret = pthread_create(&tid, NULL, callback, NULL);
    if(ret != 0) {
    
        char * errstr = strerror(ret);
        printf("error1 : %s\n", errstr);
    }

    //  Output the of main thread and sub thread id
    printf("tid : %ld, main thread id : %ld\n", tid, pthread_self());

    //  Set child thread separation , After the sub thread is separated , When the child thread ends, the corresponding resources do not need to be released by the main thread 
    ret = pthread_detach(tid);
    if(ret != 0) {
    
        char * errstr = strerror(ret);
        printf("error2 : %s\n", errstr);
    }

    pthread_exit(NULL);

    return 0;
}

stay xshell Input in gcc pthread_detach.c -o a.out -lpthread Generate executable files a.out.

Then input ./a.out Execution procedure , The results are as follows :

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-yzW2rsd2-1657802699938)(C:\Users\ThinkStation K\AppData\Roaming\Typora\typora-user-images\1657802383865.png)]

It can be seen that , The execution result does not show whether the child thread is recycled .

We connect the separated sub threads ,pthread_join, If there is an error, it means that the sub thread has been successfully separated . Add code :

// On the same 
//  Set child thread separation , After the sub thread is separated , When the child thread ends, the corresponding resources do not need to be released by the main thread 
    ret = pthread_detach(tid);
    if(ret != 0) {
    
        char * errstr = strerror(ret);
        printf("error2 : %s\n", errstr);
    }

    //  After setting separation , Connect separate child threads  pthread_join()
    ret = pthread_join(tid, NULL);
    if(ret != 0) {
    
        char * errstr = strerror(ret);
        printf("error3 : %s\n", errstr);
    }

    pthread_exit(NULL);

    return 0;
}

After compiling and running, the following results are produced :

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-JQ1scZUS-1657802699939)(C:\Users\ThinkStation K\AppData\Roaming\Typora\typora-user-images\1657802678359.png)]

There was a mistake , Description separation successful .

原网站

版权声明
本文为[123_ YQH]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/199/202207161639581091.html