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

// 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);
}

时间: 2024-08-26 08:08:23

UNIX环境高级编程11.5线程终止的相关文章

UNIX环境高级编程11.6线程同步

// threads/mutex1.c 11-5 #include <pthread.h> #include <stdlib.h> #include <apue.h> struct foo { int f_count; pthread_mutex_t f_lock; int f_id; /* ... more stuff here ... */ }; struct foo* foo_alloc(int id) /* allocate the object */ { fo

(九) 一起学 Unix 环境高级编程 (APUE) 之 线程

. . . . . 目录 (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO (三) 一起学 Unix 环境高级编程 (APUE) 之 文件和目录 (四) 一起学 Unix 环境高级编程 (APUE) 之 系统数据文件和信息 (五) 一起学 Unix 环境高级编程 (APUE) 之 进程环境 (六) 一起学 Unix 环境高级编程 (APUE) 之 进程控制 (七) 一起学 Unix 环境高级编程 (APUE)

Unix环境高级编程(十二)线程控制

本章介绍了一个进程中多个线程之间如何保持数据的似有性及进程的系统调用如何与线程进行交互. 1.线程限制: Single Unix定义了一线线程操作的限制,和其他的限制一样,可以通过sysconf来查询.和其它的限制使用目的一样,为了应用程序的在不同操作 系统的可移植性. 一些限制: PTHREAD_DESTRUCTOR_ITERATIONS: 销毁一个线程数据最大的尝试次数,可以通过_SC_THREAD_DESTRUCTOR_ITERATIONS作为sysconf的参数查询. PTHREAD_K

(十) 一起学 Unix 环境高级编程 (APUE) 之 线程控制

. . . . . 之前我们在创建线程的时候都是使用的默认属性,本章主要讨论的是自定义线程的属性. 使用默认属性基本上能解决掉遇到的大部分问题,所以自定义属性在实际项目中用得比较少. 1.线程属性 <APUE>第三版 P341 表中的属性可以用来限定一个进程能创建线程的最大数量,但是限定线程数量的宏不必太当真,因为在上一篇博文中我们说过了一个线程能创建的线程的数量是受很多因素影响的,并非一定是以这几个宏值为准的. 线程属性使用 pthread_attr_t 类型表示. 1 #include &

UNIX环境高级编程笔记之线程

本章涉及到线程的一些基本知识点,讨论了现有的创建线程和销毁线程的POSIX.1原语,此外,重点介绍了线程同步问题,讨论了三种基本的同步机制:互斥量.读写锁.条件变量.

UNIX环境高级编程11.6.4条件变量

#include <pthread.h> struct msg { struct msg *m_next; /* ... more stuff here ... */ int m_id; }; msg* workq; pthread_cond_t qready = PTHREAD_COND_INITIALIZER; pthread_mutex_t qlock = PTHREAD_MUTEX_INITIALIZER; void process_msg(void) { msg* mp; for (

Unix 环境高级编程 (APUE) 之 网络 IPC:套接字

一起学 Unix 环境高级编程 (APUE) 之 网络 IPC:套接字 . . . . . 目录 (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO (三) 一起学 Unix 环境高级编程 (APUE) 之 文件和目录 (四) 一起学 Unix 环境高级编程 (APUE) 之 系统数据文件和信息 (五) 一起学 Unix 环境高级编程 (APUE) 之 进程环境 (六) 一起学 Unix 环境高级编程 (APU

(十二) 一起学 Unix 环境高级编程 (APUE) 之 进程间通信(IPC)

. . . . . 目录 (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO (三) 一起学 Unix 环境高级编程 (APUE) 之 文件和目录 (四) 一起学 Unix 环境高级编程 (APUE) 之 系统数据文件和信息 (五) 一起学 Unix 环境高级编程 (APUE) 之 进程环境 (六) 一起学 Unix 环境高级编程 (APUE) 之 进程控制 (七) 一起学 Unix 环境高级编程 (APUE)

(十一) 一起学 Unix 环境高级编程 (APUE) 之 高级 IO

. . . . . 目录 (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO (三) 一起学 Unix 环境高级编程 (APUE) 之 文件和目录 (四) 一起学 Unix 环境高级编程 (APUE) 之 系统数据文件和信息 (五) 一起学 Unix 环境高级编程 (APUE) 之 进程环境 (六) 一起学 Unix 环境高级编程 (APUE) 之 进程控制 (七) 一起学 Unix 环境高级编程 (APUE)