Chapter 9-threaded programming (2) _ thread creation and termination

2.  thread creation and termination

(1) create a function: pthread_create

head File

#include <pthread.h>

function

int pthread_create(pthread_t* tidp, const pthread_attr_t* attr, void*(*start_rtn)(void*),void* arg);

return value

Successful return 0, otherwise it returns an error number

parameter

(1) tidp: thread identifier pointer

(2) attr: the thread pointer property

The starting address of the thread running function: (3) start_rtn

(4) arg: argument passed to the thread running function

Remark

(1) newly created thread begins execution at address start_rtn function

(2) does not guarantee the order of execution of the calling thread and the new thread.

[Programming] experiment Tortoise and the Hare

//pthread_race.c

Copy the code

#include <pthread.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 

// Example: The Tortoise and the Hare 
// Compile options: gcc -o bin / pthread_race src / pthread_race. -lpthread C 

// thread function defined parameter types 
typedef struct 
{ 
    char name [20 is]; // name of the thread 
    int time; // sleep time 
    int start; // start point 
    int end; // end 
} RaceArg; 

// custom thread run function 
void * th_fn (void * Arg) 
{ 
    RaceArg = RA * (* RaceArg) Arg; 
    int I = RA-> Start; 

    for (; I <= RA-> End; I ++) { 
        the printf ( "% S (% LX)% D RUN \ n-", RA-> name, to pthread_self (), I); 
 
        usleep (RA-> Time); // Time microseconds 
    }

    return (void *) (ra-> end - ra-> start); // returns ran away. 
    // also can call pthread_exit ((void *) (ra -end - ra-start)); to terminate the thread 
} 

int main (void) 
{ 
    srand48 (Time (NULL)); // seed random, pseudo-random generator is initialized drand48 
 
    int ERR = 0; 
    pthread_t rabbit, turtle; // define hare and the tortoise thread 
  
    // passed to thread function parameters (drand48 return value between a double random number between 0-1) 
    RaceArg R_A = { "Rabbit", (int) (drand48 () * 10 * 1000 * 10), 20 is, 50}; 
    RaceArg t_a = { "Turtle", (int) (drand48 () * 10 * 1000 * 10), 10, 60}; 
    
    // Create rabbit threads (rabbit) 
    IF (ERR = pthread_create (& rabbit, NULL, th_fn, (void *) & R_A) = 0) {! 
        perror ( "pthread_create error"); 
    } 

    // create threads tortoise (turtle) 
    IF (ERR = pthread_create (& Turtle, NULL, th_fn, (void *) &t_a) != 0){
        perror ( "pthread_create error"); 
    } 

    // wait for the end of the thread rabbit 
    int * RET = NULL; 
    pthread_join (rabbit, (void **) & RET); // pthread_join (rabbit, NULL); do not care about a child thread return value 
    printf ( "The Rabbit's Distance IS% d \ the n-", (int) RET); 
    
    // wait turtle thread end 
    pthread_join (turtle, (void **) & RET); 
    printf ( "turtle's Distance IS% d \ the n-", (int) RET ); // pthread_join (Turtle, NULL); 

    the printf ( "main Thread ID: LX% \ n-", to pthread_self ()); 
    the printf ( "! main Thread Finished \ n-"); 

    return 0; 
}

Copy the code

(2) thread termination

  ① Active Termination: execution thread function calls return statement or pthread_exit ()

  ② passive termination: the thread can be canceled by other threads in the same process (call pthread_cancel (pthid)).

  ③ termination of related functions

head File

#include <pthread.h>

function

pthread_cancel int (pthread_t tid);  // terminate the thread tid, tid passive termination

pthread_exit int (void * retval);  // terminate initiative

pthread_join int (pthread_t TH, void ** thread_return); // wait for the end of the thread

return value

Successful return 0, otherwise it returns an error number

parameter

(1) tid: To terminate the thread identifier

(2) retval: pthread_exit caller thread return value, and other functions can be used to detect pthread_join get.

(3) th: child thread identifier pthread_join waiting.

(4) thread_return: custom pointer, for storing the value of the waiting thread is returned.

Remark

(1) pthread_cancel: thread can be canceled by other processes in the same process.

(2) Because multiple threads share data segment of a process, it is often after the thread exits, exit the resources occupied by a thread and the thread will not end with the release, so you need to call pthread_join function to wait for the end of the thread, similar to wait system call.

Terminate programming experiments [thread]

//pthread_term.c

Copy the code

#include <pthread.h> 
#include <stdio.h> 
#include <stdlib.h> 

/ * Get the thread to terminate the return value * / 

// parameter defines the function thread 
typedef struct 
{ 
    int DATAl; 
    int DATA2; 
} Arg; 

// define thread function 
void * th_fn (void * Arg) 
{ 
    Arg RET * = (Arg *) Arg; 
    // return (void *) (R-> DATAl + R-> DATA2); 
    return RET; 
} 

int main ( ) 
{ 
    int ERR = 0; 
    pthread_t TH; 
    Arg R & lt = {20 is, 50}; 

    // create a new thread 
    ! IF ((ERR = pthread_create (& TH, NULL, th_fn, (void *) & R & lt)) = 0) { 
        perror ( "pthread_create error"); 
    } 

/ * 
    // the return value of the acquired sub-thread
    int * result = NULL; // because the child thread return value is void *, when the incoming pthread_join 
                        // itself will be changed to the value of the Result sub-thread returns, not in is NULL. 
                        // this is the object of the two incoming pointer, changing the value of the result itself. 
    pthread_join (TH, (void **) & Result); 
    the printf ( "% D Result IS \ n-", (int) Result); 
* / 

    // the return value of the sub-thread acquired 
    Arg * Result = NULL; 
    pthread_join (TH, ( * void) & Result); 
    the printf ( "% D Result IS \ n-", result-> result- DATAl +> DATA2); 

    return 0; 
}
Published 25 original articles · won praise 4 · views 20000 +

Guess you like

Origin blog.csdn.net/zhou8400/article/details/98055999