Linux process and thread two

Source: Internet
Author: User

int pthread_join (pthread_t thr,void * *thr_return);p thread_join function is used to suspend the current thread until the thread specified by th Terminates. If the return value of another thread is not null, it is saved in the Thr_return address. The memory resources used by one thread are not reassigned until the Pthread_join call is applied, so the Pthread_join function must be called once for each thread (except for the separated thread). Other threads cannot apply pthread_join calls to the consent Thread. Pthread_join function successfully returns 0, failure to return error code parameter Thr_return is the return value of thread th This function is typically invoked in a control thread to wait for other threads to return, similar to the wait () function in the Process. after a thread exits (in the case where the control thread does not exit), the memory resource used by the thread is not freed and needs to be freed by calling the Pthread_join () function . 
//Use of Pthread_join#include <stdio.h>#include<stdlib.h>#include<string.h>#include<unistd.h>#include<errno.h>#include<pthread.h>void* MYFUNC (void*Arg) {    int*p = (int*)malloc(sizeof(int)); if(p = =NULL) {printf ("failed to allocate memory! \ n"); returnNULL; }    *p =5; returnp;}void* MYFUNC2 (void*Arg) {    int*p = (int*)malloc(sizeof(int)); if(p = =NULL) {printf ("failed to allocate memory! \ n"); returnNULL; }    *p =Ten; Pthread_exit (p);}intMainintArgChar*Args[])    {pthread_t thr1, thr2; if(pthread_create (&thr1, null, Myfunc, null)! =0) {printf ("Create thread is failed! error message:%s\n", Strerror (errno)); return-1; }    if(pthread_create (&thr2, null, Myfunc2, null)! =0) {printf ("Create thread is failed! error message:%s\n", Strerror (errno)); return-1; }    int*RETURNNUM1 =NULL; int*returnnum2 =NULL; /*Pthread_join () function The main volume of the function 1. suspends the control thread and waits for the specified thread to return 2. Frees the memory resources of the specified thread (the thread exits without releasing the resources it consumes, unless the process exits)*/Pthread_join (thr1, (void* *) &returnnum1); if(returnnum1! =NULL) {printf ("thr1 return number is%d\n", *returnnum1); } pthread_join (thr2, (void* *) &returnnum2); if(returnnum2! =NULL) {printf ("THR2 return number is%d\n", *returnnum2); }    return 0;}

int Pthread_detach (pthread_t th);p the Thread_detach function to leave the thread in a detached state. For a disconnected thread, the colleague is not interested in the return value of the thread, you can set the thread to be detached, and the system will automatically reclaim the resources it consumes when the process Exits. A thread cannot call Pthread_detach itself and change itself to a detached state, which can only be called by other threads Pthread_detach. once a thread becomes a detachable thread, it is no longer possible to use the PTHREAD_JOISN () function because it is meaningless. Pthread_detach () returned 0 successfully and failed to return an error Code. The thread is in a detached state, the control thread exits, the detached thread exits, and the detached thread simply does not need to use the Pthread_join function to free memory Resources. use scenario for separable threads 1. The main thread does not need to wait for a child thread 2. The main thread does not care about the return code of the child threads
//Use of Pthread_detach#include <stdio.h>#include<stdlib.h>#include<string.h>#include<unistd.h>#include<errno.h>#include<pthread.h>void* MYFUNC (void*Arg) {     while(1) {printf ("fly with me \ n"); Sleep (1); }    returnNULL;}intMainintArgChar*Args[])    {pthread_t thr1; if(pthread_create (&thr1, null, Myfunc, null)! =0) {printf ("Create thread is failed! error message:%s\n", Strerror (errno)); return-1;    } Pthread_detach (thr1); Sleep (3); printf ("main end \ n"); return 0;}

int pthread_cancel (pthread_t th);p The Thread_cancel function allows one thread to cancel the second specified by another thread function successfully returns 0, and the failure returns non 0.
//Use of Pthread_join#include <stdio.h>#include<stdlib.h>#include<string.h>#include<unistd.h>#include<errno.h>#include<pthread.h>void* MYFUNC (void*Arg) {     while(1) {printf ("fly with me \ n"); Sleep (1); }    int*p =malloc(sizeof(int)); *p =7; returnp;}void* MYFUNC2 (void*Arg) {    if(arg==NULL) {printf ("param isn't Allow null!\n"); returnNULL; } Sleep (5);    pthread_t thr; THR=* (pthread_t *) arg;    Pthread_cancel (thr); int*p =malloc(sizeof(int)); *p =8; returnp;}intMainintArgChar*Args[])    {pthread_t thr1, thr2; if(pthread_create (&thr1, null, Myfunc, null)! =0) {printf ("Create thread is failed! error message:%s\n", Strerror (errno)); return-1; }    if(pthread_create (&thr2, NULL, Myfunc2, &thr1)! =0) {printf ("Create thread is failed! error message:%s\n", Strerror (errno)); return-1; }    int*NUMX1 =NULL; int*NUMX2 =NULL; Pthread_join (thr1, (void* *) &numx1); Pthread_join (thr2, (void* *) &numx2); /*program error, thread thr1 is forced to cancel in execution, return value NUMX1 is not null*/    /*if (numx1! = NULL) {printf ("thr1 return code is%d\n", *numx1);        Free (numx1);    NUMX1 = NULL; }    */    if(numx2! =NULL) {printf ("thr2 return code is%d\n", *numx2);  free(numx2); NUMX2=NULL; } printf ("main end \ n"); return 0;}

Linux process and thread two

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.