Multithreading-cancel Detailed

Source: Internet
Author: User

Multithreading-cancel Detailed

When a request to cancel a peer thread is agreed, there is a cancellation process that occurs asynchronously with the return of Pthread_cancel ().

The cancellation type and cancellation state of the target thread determine when the cancellation really occurs. The cancellation status describes the thread's cancellation status as either canceled or non-canceled.

The type of cancellation of a thread determines the ability of a thread to continue executing after receiving a cancellation request. The status and type of the cancellation are set dynamically by the thread itself.

The cancellation state and type of the calling thread are set by Pthread_setcancelstate () and Pthread_setcanceltype ().

Pthread_setcancelstate () sets the calling thread to the status of the cancellation specified by state, and returns the previous status in Oldstate.

Pthread_setcanceltype () sets the calling thread to the type of the kind that is specified for the cancellation and returns the previous type in Oldtype.

Invocation form

#include <pthread.h> int pthread_setcancelstate (int state, int *oldstate);

int pthread_setcanceltype (int type, int *oldtype);

The values of state and oldstate used to set the thread cancellation status are:

Pthread_cancel_disable

Pthread_cancel_enable

Pthread_cancel_disable causes the thread to ignore the cancellation request.

Pthread_cancel_enable allows the thread to allow cancellation of the request.

Pthread_cancel_enable is the default state for any newly created thread.

If successful, the function returns 0. If it does not succeed, the function returns an error number.

If a valid state value is not passed, pthread_setcancelstate () fails.

The function Pthread_setcanceltype () sets the cancellation state of the calling thread to type, and returns the previous state through Oldtype.

The values for type and oldtype can be:

Pthread_cancel_deffered

Pthread_cancel_asynchronous

Pthread_cancel_deffered causes the thread to postpone termination,

Until it reaches its point of cancellation. This is the default type of cancellation for any newly created thread.

Pthread_cancel_asynchronous causes the thread to terminate immediately.

If successful, the function returns 0. If unsuccessful, the function returns an error number.

If a valid type is not passed, Pthread_setcanceltype () fails.

Pthread_setcancelstate () and Pthread_setcanceltype () are used together to establish the cancellation of a thread.

Multithreading-cancel Detailed

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.