Linux Thread Foundation functions

Source: Internet
Author: User

1. Thread ID:

(1) Comparison of two thread IDs;

#include <pthread.h>int  pthread_equal (pthread_t tid1, pthread_t tid2); RET-Returns a value other than 0 if equal, or 0 value

(2) Gets the thread's own ID;

#include <pthread.h>pthread_t pthread_self (void); RET-thread ID of the calling thread

2. Thread creation:

#include <pthread.h>int pthread_create (pthread_t *restrict TIDP,//Return the ID of the thread

Const pthread_attr_t *restrict attr,//thread property, default = NULL

void * (*START_RTN) (void),//thread function entry address

void *restrict arg);//parameter ret-successful return 0 failure return error number

3. Termination of the thread:

(1) The thread is simply returned from the startup routine, and the return value is the thread's exit code;

(2) A thread can be canceled by another thread in the same routine;

(3) thread calls Pthread_exit.

#include <pthread.h>void pthread_exit (void *rval_ptr);

Rval_ptr is an untyped pointer, similar to a single parameter passed to a startup routine, where other threads in the process can access the pointer by calling the Pthread_join function;

#include <pthread.h>intvoid **rval_ptr);

Ret-successfully returned 0 otherwise the error number is returned

The calling thread has been blocked, knowing that the specified thread called Pthread_exit, returned from the startup routine, or was canceled, and that if the thread is only returned from his startup routine, Rval-_ptr will contain the return code; If the thread is canceled, the Rval_ The memory unit specified by PTR is set to pthread_canceled.

If the thread is already in the detached state, pthread_t will call the failure and return einval.

If you are not interested in the return value of the thread, RVAL_PRT is set to null. In this case, the call to Pthread_join waits for the thread to terminate, but does not get the terminating state of the thread.

4. Thread Cancellation:

#include <pthread.h>int  pthread_cancel (pthread_t tid); RET-Successful return 0 failure return error code

The function makes the thread behavior identified by the TID behave as if the parameter is called the Pthread_exit function of Pthread_canceld, but the thread can choose to ignore the cancellation mode or control the cancellation. The function does not wait for the thread to terminate, it is simply a request;

5. Thread Cleanup handler function:

#include <pthread.h>void pthread_cleanup_push (Void (*RTN) (voidvoid *  ARG); void pthread_cleanup_pop (int execute); Call to delete the cleanup program for the last push

When the thread executes the following action, the cleanup function is called, and the caller parameter is ARG, and the order of the cleanup function Rtn is scheduled by Pthread_cleanup_pus.

A. Call pthread_exit;

B. Want to use the cancellation request;

C. Call Pthread_cleanup_pop with a non-zero execute parameter;

If the execute=0 function is not called;

Note that the cleanup function is not called when normal returns from the thread;

Linux Thread Foundation functions

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.