pthread_create() function for multithreading

Overview: pthread_createIt is a function of creating threads in operating systems such as (Unix, Linux, Mac OS X). Its function is to create a thread (actually, it is to determine the entry point of calling the thread function) , and after the thread is created, it starts to run the related thread function.
pthread_createThe return value indicates success, and returns 0; indicates an error, and returns -1.

pthread_createHow functions create threads

Function prototype declaration:

#include <pthread.h>
int pthread_create(
                 pthread_t *restrict tidp,   //新创建的线程ID指向的内存单元。
                 const pthread_attr_t *restrict attr,  //线程属性,默认为NULL
                 void *(*start_rtn)(void *), //新创建的线程从start_rtn函数的地址开始运行
                 void *restrict arg //默认为NULL。若上述函数需要参数,将参数放入结构中并将地址作为arg传入。
                  );

1. Problems with passing parameters

Problem:
Avoid passing the changed amount directly in the passed parameter , otherwise the result will be unpredictable.
Even if it is another single thread, the variable value acquired by the thread may have been modified by the main thread when the thread does not acquire the passed parameter.

The usual solution:
re-apply for a piece of memory, store the parameters that need to be passed, and then pass the address as arg.

2. Pay attention to prevent memory leaks when using

By default, pthread_createthe thread created by the function is non- detached attribute, which is determined by the second parameter of the pthread_create function . In the non-detached case , when a thread ends, the system resources it occupies are not completely real. release, nor does it actually terminate .

Only when the pthread_joinfunction returns does the thread release its own resources.
Or if the detached attribute is set, the end of a thread will immediately release the resources it occupies.

If you want to ensure that there is no memory leak after creating a thread, you must use the following methods to standardize pthread_createthe use:

1. Set the thread to be detached (detached attribute)

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); //第二个参数决定了分离属性

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

However, in the comments on the blog: https://blog.csdn.net/qiurisuixiang/article/details/6648213 , someone mentioned:
Comment by

2. Use pthread_join function together

pthread_join()The function blocks the calling thread until the specified thread terminates. After pthread_join()returning, the application can reclaim any data storage space associated with the terminated thread.
However, at the same time, it should be noted that it must be used in conjunction with a thread created above, which can also play a role in mutual exclusion. Otherwise, multiple threads may preempt CPU resources, resulting in indeterminate running results.

Niu Ke a question: What is the output of the following program? (Can not be sure)

#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:
Before pthread_join(), 3 threads have been submitted, they may have been executed in sequence and randomly, or not, so the result is also unpredictable. But this can also play a role in reclaiming memory, right?

//这样才是按顺序的。
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);

Added: pthread_join() function

Function prototype:

int pthread_join(
               pthread_t tid, //需要等待的线程,指定的线程必须位于当前的进程中,而且不得是分离线程
               void **status  //线程tid所执行的函数返回值(返回值地址需要保证有效),其中status可以为NULL
                 );

pthreadNon- linuxsystem default library, you need to manually link -thread library-lpthread

Return value:
The call succeeds and returns 0.
ESRCH
Description: No thread corresponding to the given thread ID was found. (If multiple threads wait for the same thread to terminate, all waiting threads will wait until the target thread terminates. Then one waiting thread returns successfully. The remaining waiting threads will fail with an ESRCH error)
EDEADLK
Description: A deadlock will occur, as one thread waiting itself, or thread A and thread B wait for each other.
EINVAL
Description: The thread corresponding to the given thread ID is a detached thread.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324688713&siteId=291194637