Linux-pthread_join () use

In my current stock of knowledge, my understanding of threads of execution that is: Create a process a thread, the thread calls a function, you can perform some other work by the function that that thread in the process, or auxiliary process work. Since the CPU is single core, generally speaking as long as the process has been in the process of running the main program, the function that thread will not be implemented because the process does not leave time for a thread of execution. To make a thread of execution, the general operation is to make the process of sleep for some time, through sleep()can be achieved function, during which time the process is sleeping, the thread can run. It is usually dormant process created after completion of thread, there is a way for threads to run to the steps specified by the thread of execution, which is pthread_join()a function.
Once you've created a thread in the process of calling pthread_join()can make the process of waiting thread is finished, this function is a thread blocking function, call it the process will wait until the thread is finished so far, but when the function returns, resource threads will be recovered.
If the end of a thread running but not join, then the state of this thread is like Zombie Process process, it is generally best to use pthread_join()to wait for a thread to finish, and you can get the return value of the thread and release the thread resources.
Prototype:

#include <pthread.h>

int pthread_join( pthread_t thread, void **value_ptr );
  • thread: id of the process to wait for the thread, the thread id can get when you create a thread
  • value_ptr: a user-defined pointer, for storing the return value of the thread, typically define a pointer, a pointer to the address stored in the return value of the thread. such as:
void *p;
error = pthread_join( tid, &p );
  • Return Value: returns 0 if successful blocked; the number of unsuccessful returns an error, the variable that receives the return value typically designated as error, so that its meaning is easy to understand, the code may be determined by the success or not of blocking:
if( error )
{
	printf("thread join failed!\n");
}
Published 62 original articles · won praise 188 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_43743762/article/details/101097144