pthread_join () Detailed and experiments

pthread_join () Function Prototype:

int pthread_join(pthread_t thread, void **retval);
args:
    pthread_t thread: 被连接线程的线程号
    void **retval : 指向一个指向被连接线程的返回码的指针的指针
return:
    线程连接的状态,0是成功,非0是失败

1. Why use pthread_join ()

In many cases, the main thread to create and start a child thread, if the child thread to a lot of time-consuming operation, often before the child thread the main thread will end, but if the main thread has finished the deal with other matters, need to deal with the results of the sub-thread, which is the main thread needs to wait longer after the end of sub-thread execution is completed, this time we should use pthread_join () method has.
That action pthread_join () may be understood: the main thread wait terminator thread. That is, sub-thread calls pthread_join () method code behind, and only until the end of the sub-thread can execute.

When A thread calls Thread B and pthread_join (), A thread is blocked until the end of the thread B, A thread will continue execution. When pthread_join () function returns, the calling thread is considered the end in the true sense, its memory space is freed (if the calling thread is non-isolated). Here are three things to note:

  1. Released memory space is only space systems, you must manually clear the space allocated to the program, such as malloc () space allocation.
    2. A thread can only be connected to a thread.
    3. Thread the connection must be non-isolated, or the connection will be wrong.
    It can be seen pthread_join () has two effects:

  2. Waiting for the other end of the thread: When you call pthread_join (), the current thread is blocked until after the end of the thread is called, the current thread will resume execution.

  3. Thread resources for recycling: If a thread is non-isolated (threads created by default are non-isolated) and does not use pthread_join the thread (), then after the end of the thread does not release its memory space, which can lead to this thread has become a "zombie thread."

2. use

tid pthread_t;
pthread_create (& tid, NULL, thread_run, NULL);
pthread_join (tid, NULL);
calling pthread_join method directly after creating a thread on the line.

3. Experiment Code

You can see the effect performed by the code, we know:

#include "stdafx.h" 
#include <pthread.h> 
#include <stdio.h> 
#include <Windows.h> 
#pragma comment(lib, "pthreadVC2.lib") 

static int count = 0; 

void* thread_run(void* parm) 
{ 
for (int i=0;i<5;i++) 
{ 
count++; 
printf("The thread_run method count is = %d\n",count); 
Sleep(1000); 
} 
return NULL; 
} 

int main() 
{ 
pthread_t tid; 
pthread_create(&tid, NULL, thread_run,NULL); 
// 加入pthread_join后,主线程"main"会一直等待直到tid这个线程执行完毕自己才结束 
// 一般项目中需要子线程计算后的值就需要加join方法 
pthread_join(tid,NULL); 
// 如果没有join方法可以看看打印的顺序 
printf("The count is = %d\n",count); 
getchar(); 
return 0; 
}

Plus the pthread_join () method to print:

If the inside of the pthread_join () method of printing commented:

You can see that, if there is no increase pthread_join () method, main thread which directly execute from left, are added back after waiting thread executes only after execution of code.



 

Published 136 original articles · won praise 71 · views 160 000 +

Guess you like

Origin blog.csdn.net/u012308586/article/details/104680338