美文网首页
APUE// linux 线程创建及退出

APUE// linux 线程创建及退出

作者: ustclcl | 来源:发表于2019-03-14 00:27 被阅读0次
    1 // APUE example 11-2
    2 // lcl 20190314
    3
    4 #include "../myapue.h"
    5 #include <pthread.h>
    6
    7 pthread_t ntid;
    8
    9 void printids(const char *s)
    10 {
    11     pid_t       pid;
    12     pthread_t   tid;
    13
    14     pid = getpid();
    15     tid = pthread_self();
    16     printf("%s pid %lu tid %lu (0x%lx)\n",s,(unsigned long)pid,(unsigned long)tid,(unsigned long)tid);
    17 }
    18
    19 void * thr_fn(void *arg)
    20 {
    21     printids("new thread: ");
    22     return ((void*)0);
    23 }
    24
    25 int main (void)
    26 {
    27     int     err;
    28     err = pthread_create(&ntid, NULL, thr_fn, NULL);
    29     if (err != 0)
    30       printf("can't create thread\n");
    31     printids("main thread: ");
    32     sleep(1);
    33     exit(0);
    34 }
    

    编译时需要链接pthread,因为pthread不是标准库

    [root@f3c6f9f95723 ch11]# gcc pthr.c -lpthread -g -o  pthr
    [root@f3c6f9f95723 ch11]# ./pthr
    main thread:  pid 63 tid 140407368517440 (0x7fb323530740)
    new thread:  pid 63 tid 140407360116480 (0x7fb322d2d700)
    

    线程退出,接收退出码

     1 // APUE example 11-2
      2 // lcl 20190304
      3
      4 #include "../myapue.h"
      5 #include <pthread.h>
      6
      7 void *thr_fn1(void *arg)
      8 {
      9     printf("thread 1 returning\n");
     10     return ((void *) 1);
     11 }
     12
     13 void *thr_fn2(void *arg)
     14 {
     15     printf("thread 2 exiting\n");
     16     pthread_exit((void *)2);
     17 }
     18
     19 int main(void)
     20 {
     21     int     err;
     22     pthread_t tid1,tid2;
     23     void    *tret;
     24
     25     err = pthread_create(&tid1, NULL, thr_fn1, NULL);
     26     if(err != 0)
     27         printf("err %d, can't create thread 1\n",err);
     28     err = pthread_create(&tid2, NULL, thr_fn2, NULL);
     29     if (err != 0)
     30         printf("err %d, can't create thread 2\n",err);
     31     err = pthread_join(tid1, &tret);
     32     if (err != 0)
     33         printf("err %d, can't join with thread1\n",err);
     34     printf("thread 1 exit code %ld\n",(long)tret);
     35     err = pthread_join(tid2, &tret);
     36     if (err != 0)
     37         printf("err %d, can't join with thread2\n",err);
     38     printf("thread 2 exit code %ld\n",(long)tret);
     39     exit(0);
     40 }
    
    [root@f3c6f9f95723 ch11]# gcc get_exit_of_thread.c -g -lpthread -o get_exit_of_thread
    [root@f3c6f9f95723 ch11]# ./get_exit_of_thread
    thread 1 returning
    thread 1 exit code 1
    thread 2 exiting
    thread 2 exit code 2
    

    线程退出时返回地址,此地址在线程退出后被覆盖的例子

    // APUE example 11-4                                              
    // lcl 20190314                                                   
    /******************************************                       
    [root@f3c6f9f95723 ch11]# ./exit_thread_err                       
     structure at 0x7f33a3ce7f00                                      
     foo.a = 1                                                        
     foo.b = 2                                                        
     foo.c = 3                                                        
     foo.d = 4                                                        
    parent create a second thread:                                    
    thread 2: ID is 139859768280832                                   
     structure at 0x7ffebf271120                                      
     foo.a = -1546748160                                              
     foo.b = 32563                                                    
     foo.c = -1087952529                                              
     foo.d = 32766                                                    
    ******************************************/                       
    #include "../myapue.h"                                            
    #include <pthread.h>                                              
                                                                      
    struct foo {                                                      
        int a, b, c, d;                                               
    };                                                                
                                                                      
    void printfoo(const char *s, const struct foo *fp)                
    {                                                                 
        printf("%",s);                                                
        printf(" structure at 0x%lx\n", (unsigned long)fp);           
        printf(" foo.a = %d\n",fp->a);                                
        printf(" foo.b = %d\n",fp->b);                                
        printf(" foo.c = %d\n",fp->c);                                
        printf(" foo.d = %d\n",fp->d);                                
    }                                                                 
                                                                      
    void *thr_fn1(void *arg)                                          
    {                                                                 
        struct foo foo1 = {1,2,3,4};                                  
        printfoo("thread 1:\n", &foo1);                               
        pthread_exit((void*)&foo1);                                   
    }                                                                 
                                                                      
    void *thr_fn2(void *arg)                                          
    {                                                                 
       printf("thread 2: ID is %lu\n",(unsigned long)pthread_self()); 
       pthread_exit((void *)0);                                       
    }  
                                                                     
    int main(void)                                                   
    {                                                                
        int     err;                                                 
        pthread_t tid1,tid2;                                         
        struct foo *fp;                                              
        err = pthread_create(&tid1, NULL, thr_fn1, NULL);            
        if(err != 0)                                                 
             printf("err %d, can't create thread 1\n",err);          
        err = pthread_join(tid1, (void*)fp);                         
        if (err != 0)                                                
            printf("err %d, can't join with thread1\n",err);         
        sleep(1);                                                    
                                                                     
        printf("parent create a second thread:\n");                  
        err = pthread_create(&tid2, NULL, thr_fn2, NULL);            
        if (err != 0)                                                
            printf("err %d, can't create thread 2\n",err);           
        sleep(1);                                                    
                                                                     
        printfoo("parent:\n",fp);                                    
        exit(0);                                                     
    }                                                                
                                                                                                                                    
    

    相关文章

      网友评论

          本文标题:APUE// linux 线程创建及退出

          本文链接:https://www.haomeiwen.com/subject/urhfmqtx.html