zoukankan      html  css  js  c++  java
  • UNIX环境高级编程11.5线程终止

    }M_JZ@MP$]R6RK__Q9}VV)4

    Z)_3YF5AGTTBS_KTR8OJXFI

    // threads/exitstatus.c 11-2
    #include "apue.h"
    #include <pthread.h>
    
    void* thr_fn1(void* arg)
    {
        printf("thread 1 returning
    ");
        /* return a variable of type void*
        return a pointer who points to a variable of type void*/
        return((void*)10);
    }
    
    void* thr_fn2(void* arg)
    {
        printf("thread 2 exiting
    ");
        pthread_exit((void *)21);
    }
    
    /*
     p is a pointer to an pointer B
     B points to an vaviable with type void
     */
    void test2(void** p)
    {   /*
         void* tret;
         void** p = &tret;
    
         *p is equal to variable tret
         tret = 'n'
         test2 successfully modify the vaviable of tret
         just like 
         tret = (void*)'n';
         in outside of this function
         tret is treated as a variable of type char
         so the compiler treate the memory of &tret as a variable of char
         and the content (of course one byte) is the asc code of char 'n'
        */
        *p = (void*)'n'; // don't think too much, this is just a type conversion
    }
    
    int main(void)
    {
        int       err;
        pthread_t tid1;
        pthread_t tid2;
        void*     tret; /* define a pointer to void*/
    
        err = pthread_create(&tid1, NULL, thr_fn1, NULL);
        if (err != 0)
            err_quit("can't create thread 1: %s
    ", strerror(err));
        err = pthread_create(&tid2, NULL, thr_fn2, NULL);
        if (err != 0)
            err_quit("can't create thread 2: %s
    ", strerror(err));
        /* pass an address of a vaviable, may change the value of the it*/
        err = pthread_join(tid1, &tret);
        if (err != 0)
            err_quit("can't join with thread 1: %s
    ", strerror(err));
        /*
         force a variable with type void* to int
         force a pointer to an int
         */
        printf("thread 1 exit code %d
    ", (int)(long)tret);
        err = pthread_join(tid2, &tret);
        if (err != 0)
            err_quit("can't join with thread 2: %s
    ", strerror(err));
        printf("thread 2 exit code %d
    ", (int)(long)tret);
    
        test2(&tret);
        printf("%c
    ", (char)(long)tret); // treat tret as a variable of type char
                                          // just like the following
    
        int a = 10;
        int* pp = &a;
        *pp = (int)'n'; // just like a = (int)'n'
        printf("%c 
    ", (char)a);
    
        return 0;
    }
    

    K2Y)($7S69M)%UPM@RI31JD

    ZW}@NC48)4G62E66F)`ICE4

    // threads/badexit2.c 11-3
    #include "apue.h"
    #include <pthread.h>
    
    struct foo {
        int a;
        int b;
        int c;
        int d;
    };
    
    void printfoo(const char *s, const struct foo *fp)
    {
        printf(s);
        printf("  structure at 0x%x
    ", (unsigned int)(long)fp);
        printf("  foo.a = %d
    ", fp->a);
        printf("  foo.b = %d
    ", fp->b);
        printf("  foo.c = %d
    ", fp->c);
        printf("  foo.d = %d
    ", fp->d);
    }
    
    void * thr_fn1(void *arg)
    {
        struct foo foo = {1, 2, 3, 4};
        printfoo("thread 1:
    ", &foo);
        pthread_exit((void *)&foo);
    }
    
    void * thr_fn2(void *arg)
    {
        printf("thread 2: ID is %d
    ", (int)pthread_self());
        pthread_exit((void *)0);
    }
    
    int main(void)
    {
        int err;
        pthread_t tid1;
        pthread_t tid2;
        struct foo* fp;
    
        err = pthread_create(&tid1, NULL, thr_fn1, NULL);
        if (err != 0)
            err_quit("can't create thread 1: %s
    ", strerror(err));
        // err = pthread_join(tid1, (void*)&fp);
        err = pthread_join(tid1, (void**)&fp);
        if (err != 0)
            err_quit("can't join with thread 1: %s
    ", strerror(err));
        sleep(1);
        printf("parent starting second thread
    ");
        err = pthread_create(&tid2, NULL, thr_fn2, NULL);
        if (err != 0)
            err_quit("can't create thread 2: %s
    ", strerror(err));
        sleep(1);
        printfoo("parent:
    ", fp);
        exit(0);
    }
    

    5ZU%3%MHV2PR256[}NV9LIB

    {`986J)87_(AMOZQMBVZROA

     

    HXOFOOB17A)6QEFJR@04THG

    %LUH{A]35J`%0N0`V`CY98P

    #include "apue.h"
    #include <pthread.h>
    
    void cleanup(void *arg)
    {
        printf("cleanup: %s
    ", (char *)arg);
    }
    
    void* thr_fn1(void *arg)
    {
        printf("thread 1 start
    ");
        pthread_cleanup_push(cleanup, (void*)"thread 1 first handler");
        pthread_cleanup_push(cleanup, (void*)"thread 1 second handler");
        printf("thread 1 push complete
    ");
        if (arg)
            return((void *)1);
        pthread_cleanup_pop(0);
        pthread_cleanup_pop(0);
        return((void *)1);
    }
    
    void* thr_fn2(void *arg)
    {
        printf("thread 2 start
    ");
        pthread_cleanup_push(cleanup, (void*)"thread 2 first handler");
        pthread_cleanup_push(cleanup, (void*)"thread 2 second handler");
        printf("thread 2 push complete
    ");
        if (arg)
            pthread_exit((void *)2);
        pthread_cleanup_pop(0);
        pthread_cleanup_pop(0);
        pthread_exit((void *)2);
    }
    
    int main(void)
    {
        int err;
        pthread_t tid1, tid2;
        void* tret;
    
        err = pthread_create(&tid1, NULL, thr_fn1, (void *)1);
        if (err != 0)
            err_quit("can't create thread 1: %s
    ", strerror(err));
        err = pthread_create(&tid2, NULL, thr_fn2, (void *)1);
        if (err != 0)
            err_quit("can't create thread 2: %s
    ", strerror(err));
        err = pthread_join(tid1, &tret);
        if (err != 0)
            err_quit("can't join with thread 1: %s
    ", strerror(err));
        printf("thread 1 exit code %d
    ", (int)(long)tret);
        err = pthread_join(tid2, &tret);
        if (err != 0)
            err_quit("can't join with thread 2: %s
    ", strerror(err));
        printf("thread 2 exit code %d
    ", (int)(long)tret);
        return 0;
    }
    

    08WJ6_TD58K_D{DS_QPSF5F

     

    IO2TTQMRR{I~IN9406DG80V

    在看apue第三版

    ]T0AE8ODN)WRQ6NUV(H7I72

    L6{UK2V0FZSDH9BC{{5CJ8D

    2471f1ec8f8544861a6cc4c3cec0871b

  • 相关阅读:
    python基础篇-wordcloud库的使用
    python基础篇-数据格式化和处理
    python基础篇-文件(读取,操作,关闭)
    python基础篇-jieba库的使用
    python基础篇-组合数据类型-3.字典
    mbStringLength 获取javascript字符串字节数
    JS 字符unicode转换函数
    jar命令解析--转自百度知道
    JBOSS7.0 热部署及开启远程调试的方法
    WampServer 安装心得
  • 原文地址:https://www.cnblogs.com/sunyongjie1984/p/4278101.html
Copyright © 2011-2022 走看看