Pthread of multithreading_ Create() function

King of heaven and earth tiger 626 2021-03-31 11:38:59 阅读数:871

本文一共[544]字,预计阅读时长:1分钟~
pthread multithreading_ multithreading create function

executive summary :pthread_create yes (Unix、Linux、Mac OS X) And so on operating system creation thread function . Its function is to create threads ( It's actually determining the entry point to call the thread function ), After the thread is created , Start running related thread functions .
pthread_create The return value of indicates success , return 0; It means that something has gone wrong , Return to indicate -1.

pthread_create How functions create threads
Function prototype declaration :

#include <pthread.h>
int pthread_create(
                 pthread_t *restrict tidp,   // Newly created thread ID The memory unit pointed to .
                 const pthread_attr_t *restrict attr,  // Thread properties , The default is NULL
                 void *(*start_rtn)(void *), // Newly created thread from start_rtn The address of the function starts running
                 void *restrict arg // The default is NULL. If the above function requires parameters , Put the parameters in the structure and take the address as arg Pass in .
                  );

1. The problem of passing parameters
problem :
Avoid passing the changed quantity directly in the passed parameter , Otherwise, the results will be unpredictable .
Even if it's just creating a single thread , Or when the thread does not get the pass parameter , The variable value obtained by the thread has been modified by the main thread .

Usually the solution :
Re apply for a piece of memory , Store the parameters that need to be passed , Let's take this address as arg Pass in .

2. Pay attention to prevent memory leakage when using
By default through pthread_create The thread created by the function is non disjunctive , from pthread_create The second parameter of the function determines , Without separation , When a thread ends , The system resources it occupies are not really released completely , And it doesn't really end .

Only in pthread_join When function returns , This thread will release its own resources .
Or in the case of separating properties , The end of a thread immediately releases the resources it occupies .

If you want to make sure that after the thread is created , Make sure there are no memory leaks , The following methods must be adopted to regulate pthread_create Use :

1. The setup thread is detached( Separation of attributes )
void run() { 
    return;

int main(){ 
    pthread_t thread; 
    pthread_attr_t attr; 
    pthread_attr_init( &attr ); 
    pthread_attr_setdetachstate(&attr,1); 
    pthread_create(&thread, &attr, run, 0); // The second parameter determines the separation property

    //...... 
    return 0; 
}

however , In blogs :https://blog.csdn.net/qiurisuixiang/article/details/6648213 Commentary , It was mentioned that :


2. Matching use of pthread_join function
pthread_join() Function will always block the calling thread , Until the specified thread terminates . When pthread_join() After returning , An application can reclaim any data storage space associated with a terminated thread .
however , And also notice that , Be sure to use it with a thread created above , This can also play the role of mutual exclusion . Otherwise, multithreading may preempt CPU resources , The result of operation is uncertain .

Niuke, a topic : What is the output of the following program ?( Undetermined )

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

void* print1(void* data){
    printf("1 ");
}

void* print2(void* data){
    printf("2 ");
}

void* print3(void* data){
    printf("3 ");
}

int main(void){
    pthread_t t,t1,t2;

    pthread_create(&t,0,print1,NULL);
    pthread_create(&t1,0,print2,NULL);
    pthread_create(&t2,0,print3,NULL);

    pthread_join(t,NULL);
    pthread_join(t1,NULL);
    pthread_join(t2,NULL);
    printf("\n");
}

analysis :
stay pthread_join() Before ,3 All threads have committed , They may have been executed in random order , Or maybe not , So the results are unpredictable . But this can also play the role of recycling memory ?

// That's in order .
pthread_create(&t, 0, print1, NULL);
pthread_join(t, NULL);
pthread_create(&t1, 0, print2, NULL);
pthread_join(t1, NULL);
pthread_create(&t2, 0, print3, NULL);
pthread_join(t2, NULL);

Add :pthread_join() function
The function prototype :

int pthread_join(
               pthread_t tid, // Threads that need to wait , The specified thread must be in the current process , And it can't be a detached thread
               void **status  // Threads tid The function executed returns the value ( The return value address needs to be valid ), among status It can be for NULL
                 );

pthread Not linux The default library of the system , Manual link required - Thread library -lpthread

Return value :
Call successfully returned 0.
ESRCH
describe : Not found with the given thread ID The corresponding thread .( If multiple threads wait for the same thread to terminate , All waiting threads will wait until the target thread terminates . Then a wait thread returns successfully . The remaining waiting threads will fail and return ESRCH error )
EDEADLK
describe : There will be a deadlock , Like a thread waiting for itself , Or threads A And thread B Waiting for each other .
EINVAL
describe : With the given thread ID The corresponding thread is the detached thread .
 

版权声明:本文为[King of heaven and earth tiger 626]所创,转载请带上原文链接,感谢。 https://netfreeman.com/2021/03/20210327101250362y.html