UNIX Environment Advanced Programming 11.5 thread termination

Source: Internet
Author: User

THREADS/EXITSTATUS.C 11-2#include "apue.h" #include <pthread.h>void* thr_fn1 (void* Arg) {printf ("Thread 1 retur    Ning\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 a 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 are treated as a variable of type char so the compiler treate the memory of &t RET 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 (&AMP;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, could 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 (&AMP;TID1, NULL, THR_FN1, NULL);    if (err! = 0) err_quit ("Can ' t create thread 1:%s\n", strerror (err));    Err = Pthread_join (Tid1, (void*) &AMP;FP);    Err = Pthread_join (Tid1, (void**) &AMP;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 Environment Advanced Programming 11.5 thread termination

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.