Linux ---- thread creation, waiting to exit

First, thread creation and exit

1. Create a simple thread

#include <func.h>

void* threadFnc(void *p){
    printf("I am child thread\n");
}
int main(int argc,char* argv[])
{
    pthread_t pth;//创建一个线程ID
    int ret=pthread_create(&pth,NULL,threadFnc,NULL);//创建线程
    THREARD_ERRORCHECK(ret,"pthread_create");
    usleep(1);
    printf("I am main .\n");
    return 0;
}

结果:
Here Insert Picture Description
代码分析:
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);

  • The first argument refers to the thread ID,
  • The second argument is NULL, representing an average thread,
  • The third parameter indicates the sub-thread function,
  • The fourth parameter is passed to the child thread function parameters.

2, pass the thread Participation

#include <func.h>

void* threadFnc(void *p){
    printf("I am child thread\n");
    printf("%s\n",(char *)p);
}
int main(int argc,char* argv[])
{
    pthread_t pth;
    char *pArr=(char *)malloc(20);
    strcpy(pArr,"helloworld");
    int ret=pthread_create(&pth,NULL,threadFnc,pArr);
    THREARD_ERRORCHECK(ret,"pthread_create");
    
    pthread_join(pth,NULL);
    printf("I am main .\n");
    return 0;
}

Here Insert Picture Description
3. pass integer parameters

#include <func.h>

void* threadFnc(void *p){
    printf("I am child thread\n");
    printf("%d\n",*(int *)p);
}
int main(int argc,char* argv[])
{
    pthread_t pth;
    //char *pArr=(char *)malloc(20);
    //strcpy(pArr,"helloworld");
    int val=10;
    int ret=pthread_create(&pth,NULL,threadFnc,&val);
    THREARD_ERRORCHECK(ret,"pthread_create");
    
    pthread_join(pth,NULL);
    printf("I am main .\n");
    return 0;
}

result:

Here Insert Picture Description
Analysis Code:
int pthread_create (pthread_t Thread *, const * attr the pthread_attr_t is,
void * (by start_routine *) (void *), void * Arg);
last parameter passed integer address. In the thread handler to come by way of the value of a strong turn.
4. The two thread
note employed herein parameter, using (void *) strongly turn type val (64-bit systems are eight pointer type, long type is eight)

#include <func.h>

void* threadFnc(void *p){
    printf("I am child thread\n");
    
    printf("%ld\n",(long)p);
}
int main(int argc,char* argv[])
{
    pthread_t pth,pth1;
    long val=1;
    int ret=pthread_create(&pth,NULL,threadFnc,(void *)val);
    THREARD_ERRORCHECK(ret,"pthread_create");
    val=2;
    ret=pthread_create(&pth1,NULL,threadFnc,(void *)val);
    THREARD_ERRORCHECK(ret,"pthread_create");
    pthread_join(pth,NULL);
    pthread_join(pth1,NULL);

    printf("I am main .\n");
    return 0;
}

Results:
Here Insert Picture Description
code analysis: If the write function according to Embodiment 3, the output will be two findings 2.
In this way the advantage that, void * and long types are stored in the eight 64-bit system. Similarly stronger turn into a long type when printing received, or visit the cross-border phenomenon occurs.
5, the main thread and the child thread are a number of added 10000000

 #include <func.h>
    2 #define N 10000000
    3 void* threadFnc(void *p){
    4     int i=0;
    5     for(i=0;i<N;i++){
    6         *(int *)p+=1;
    7     }                                                                            
    8     printf("%d\n",*(int *)p);9 }10 int main(int argc,char* argv[])
   11 {
   12     pthread_t pth;
   13     //char *pArr=(char *)malloc(20);
   14     //strcpy(pArr,"helloworld");
   15     int val=10;
   16     int ret=pthread_create(&pth,NULL,threadFnc,&val);
   17     THREARD_ERRORCHECK(ret,"pthread_create");
   18     int i;
   19     for(i=0;i<N;i++){
   20         val+=1;
   21     }
   22     pthread_join(pth,NULL);
   23     printf("I am main .\n");
   24     return 0;
   25 }

Results:
Here Insert Picture Description
Analysis: This result is correct. Can not be added to the two million is due to the concurrent phenomenon. It can be avoided by the concurrent locking and unlocking.
6. Quit thread

    1 #include <func.h>
    2 void print(){
    3     printf("I am print.\n");
    4     pthread_exit(NULL);     //退出线程                                                     
    5 }
?   6 void* threadFnc(void *p){
    7     printf("I am child thread\n");
    8     print();
    9     printf("I am after print.\n");
   10     return NULL;
   11 }
?  12 int main(int argc,char* argv[])
   13 {
   14     pthread_t pth;
   15     int ret=pthread_create(&pth,NULL,threadFnc,NULL);
   16     THREARD_ERRORCHECK(ret,"pthread_create");
   17     pthread_join(pth,NULL);
   18     printf("I am main .\n");
   19     return 0;
   20 }

Results:
Here Insert Picture Description
code analysis:
void pthread_exit (void * retval);
for a normal return, exit thread.

Second, the thread waits exit

1. Take the child thread the main thread return value (sub-thread own application space)

  1 #include <func.h>
    2 
    3 void* threadFnc(void *p){
    4     p=malloc(20);
    5     strcpy((char *)p,"I AM GOOG!");
    6     printf("I am child thread.%s\n",(char *)p);
    7     pthread_exit(p);
    8 }
?   9 int main(int argc,char* argv[])
   10 {
   11     pthread_t pth;
   12     int ret=pthread_create(&pth,NULL,threadFnc,NULL);
   13     THREARD_ERRORCHECK(ret,"pthread_create");
   14     char *res;
   15     ret=pthread_join(pth,(void **)&res);
   16     THREARD_ERRORCHECK(ret,"pthread_join");
   17 
   18     printf("I am main .%s\n",res);                                               
   19     return 0;
   20 }

Results:
Here Insert Picture Description
code analysis:
void the pthread_exit (void * retval);
Parameter Return Value can fill, note that this is of type void *.
The main thread receives the return value pthread_join.
int pthread_join (pthread_t thread, void ** retval);

  • The first parameter represents the child process ID
  • Second parameter value stored in the received return where
    reception is successful return value 0.

2. The number of child thread returns an int

 pthread_join.c                                                          ?? buffers 
    1 #include <func.h>                                                                
    2 
    3 void* threadFnc(void *p){
    4     printf("I am child thread.val=%ld\n",(long)p);
    5     pthread_exit((void *)2);
    6 
    7 }
    8 int main(int argc,char* argv[])
    9 {
   10     pthread_t pth;
   11     long val=1;
   12     int ret=pthread_create(&pth,NULL,threadFnc,(void *)val);
   13     THREARD_ERRORCHECK(ret,"pthread_create");
   14     long res;//不能用int,否则会访问越界
   15     pthread_join(pth,(void **)&res);
   16     printf("I am main.and getval=%ld\n",res);
   17     return 0;
   18 }

Results:
Here Insert Picture Description
3. Thread Cancellation

   1 #include <func.h>
    23 void* threadFnc(void *p){
    4     printf("I am child thread\n");
    5     return NULL;
    6 }7 int main(int argc,char* argv[])
    8 {
    9     pthread_t pth;
   10     int ret=pthread_create(&pth,NULL,threadFnc,NULL);
   11     THREARD_ERRORCHECK(ret,"pthread_create");
   12     ret=pthread_cancel(pth);
   13     THREARD_ERRORCHECK(ret,"pthread_cancel");
   14     long res;
   15     ret=pthread_join(pth,(void **)&res);
   16     THREARD_ERRORCHECK(ret,"pthread_join");
   17     printf("child return :%ld\n",res);                                           
   18     printf("I am main .\n");
   19     return 0;
   20 }

The results:
Here Insert Picture Description
the Cancel success, the child thread return value is -1.
When the child is in while (1) can not be the Cancel
4, resource cleanup
application resources sub-processes and cleanup
of code:

#include <func.h>
    2 void cleanup(void *p)
    3 {
    4     free(p);
    5     printf("free end.\n");
    6 }
    7 void* threadFnc(void *p){
    8     p=malloc(20);
    9     strcpy((char *)p,"clean up");
   10     pthread_cleanup_push(cleanup,p);
   11     pthread_cleanup_pop(1);                                                      
   12     return NULL;
   13 }14 int main(int argc,char* argv[])
   15 {
   16     pthread_t pth;
   17     int ret=pthread_create(&pth,NULL,threadFnc,NULL);
   18     THREARD_ERRORCHECK(ret,"pthread_create");
   19     ret=pthread_cancel(pth);
   20     THREARD_ERRORCHECK(ret,"pthread_cancel");
   21     long res;
   22     ret=pthread_join(pth,(void **)&res);
   23     THREARD_ERRORCHECK(ret,"pthread_join");
   24     printf("child return :%ld\n",res);
   25     printf("I am main .\n");
   26     return 0;
   27 }

The results:
Here Insert Picture Description
Tips: clean-up function is popped push effect.

Published an original article · won praise 0 · Views 6

Guess you like

Origin blog.csdn.net/weixin_43574252/article/details/104900563