Linux thread (2): Termination of Thread

Source: Internet
Author: User
Tags exit in

 

If any thread in the process calls exit, _ exit, _ exit, the whole process is terminated. This is not the thread termination, but the process, which terminates all threads in the process.

 

1. Thread termination method:

A single thread can exit in three ways:

  • The return value is the exit code of the thread.
  • Canceled by other threads in the same process.
  • Call pthread_exit.

 

2. pthread_exit function:

  • Prototype: void pthread_exit (void * rval_ptr );
  • Header file: <pthread. h>
  • Parameter: rval_ptr is a non-type pointer pointing to the return value of the thread to store the variable.

 

3. pthread_join function:

  • Prototype: int pthread_join (pthread_t thread, void ** rval_ptr );
  • Header file: <pthread. h>
  • Returned value: 0 is returned if the request is successful. Otherwise, the error number is returned.
  • Parameters:
    • Thread: thread ID.
    • Rval_ptr: pointer to the returned value (the returned value is also a pointer ).
  • Note:
    • The Calling thread will be blocked until the specified Thread calls pthread_exit and is returned or canceled from the startup routine.
    • If the thread returns from its startup routine, rval_ptr contains the return code.
    • If the thread is canceled, the memory unit specified by rval_ptr is set to pthread_canceled.
    • If you do not care about the returned value, set rval_ptr to null.

 

4. instance:

Main. C code:

# Include <pthread. h>
# Include <stdio. h>


/** // * Print process and thread IDs */
Void printids (const char * s)
...{
Pid_t PID, ppid;
Pthread_t tid;

PID = getpid ();
Ppid = getppid ();
Tid = pthread_self ();

Printf ("% 16 s PID % 5u ppid % 5u TID % 16u (0x % x )",
S, (unsigned INT) PID, (unsigned INT) ppid,
(Unsigned INT) tid, (unsigned INT) tid );
}

/** // * Thread process */
Void * thread_func (void * Arg );
...{
Printids ("New thread :");
Return (void *) 108;
}

/** // * Main func */
Int main ()
...{
Int err;
Void * tret;/** // * thread return value */
Pthread_t ntid;

Err = pthread_create (& ntid, null, thread_func, null );
If (Err! = 0)
Perror ("can't create thread ");

Err = pthread_join (ntid, & tret );
If (Err! = 0)
Perror ("can't join thread ");

Printids ("main thread :");
Printf ("thread exit code: % d", (INT) tret );
Sleep (1 );

Return 0;
}

This code is adapted from the previous instance. The execution process is as follows:

  • First, create a new thread. After printing IDs, the thread returns 108.
  • Then, use pthread_join to obtain the return value of the thread, which is stored in the Tret.
  • The main thread prints IDs.
  • The main thread prints the Tret, that is, the return value of the new thread.

 

5. pthread_cancel function:

  • Prototype: int pthread_cancel (pthread_t tid );
  • Header file: <pthread. h>
  • Returned value: 0 is returned if the request is successful. Otherwise, the error number is returned.
  • Note:
    • It is equivalent to calling pthread_exit with the parameter pthread_canceled.
    • Cancel does not wait for the thread to terminate. It only initiates a request.
    • The thread can choose to ignore or control the cancellation method.

 

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.