码迷,mamicode.com
首页 > 编程语言 > 详细

UNIX环境高级编程11.5线程终止

时间:2015-02-06 23:09:43      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:

 

技术分享

 

技术分享

 

// threads/exitstatus.c 11-2
#include "apue.h"
#include <pthread.h>

void* thr_fn1(void* arg)
{
    printf("thread 1 returning\n");
    /* 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\n");
    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\n", strerror(err));
    err = pthread_create(&tid2, NULL, thr_fn2, NULL);
    if (err != 0)
        err_quit("can‘t create thread 2: %s\n", 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\n", strerror(err));
    /*
     force a variable with type void* to int
     force a pointer to an int
     */
    printf("thread 1 exit code %d\n", (int)(long)tret);
    err = pthread_join(tid2, &tret);
    if (err != 0)
        err_quit("can‘t join with thread 2: %s\n", strerror(err));
    printf("thread 2 exit code %d\n", (int)(long)tret);

    test2(&tret);
    printf("%c\n", (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 \n", (char)a);

    return 0;
}

 

技术分享

 

技术分享

 

// 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\n", (unsigned int)(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 foo = {1, 2, 3, 4};
    printfoo("thread 1:\n", &foo);
    pthread_exit((void *)&foo);
}

void * thr_fn2(void *arg)
{
    printf("thread 2: ID is %d\n", (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\n", 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\n", strerror(err));
    sleep(1);
    printf("parent starting second thread\n");
    err = pthread_create(&tid2, NULL, thr_fn2, NULL);
    if (err != 0)
        err_quit("can‘t create thread 2: %s\n", strerror(err));
    sleep(1);
    printfoo("parent:\n", fp);
    exit(0);
}

技术分享

 

技术分享

 

技术分享

 

技术分享

UNIX环境高级编程11.5线程终止

标签:

原文地址:http://www.cnblogs.com/sunyongjie1984/p/4278101.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!