. Canceling a thread

Source: Internet
Author: User

/*0. Cancel thread int pthread_cancel (pthread_t thread), set cancel point void Pthread_testcancel (void), test whether receive cancellation request, if any, end thread. Example:*/#include<stdlib.h>#include<pthread.h>#include<stdio.h>#include<sched.h>intA =0;void*thread1 (void*Arg)         {Pthread_testcancel (); This function is very important to be used in conjunction with pthread_cancel (T1); A=Ten; } /*if instead: int a = 0;void *thread1 (void *arg) {a = 10; Pthread_testcancel (); Run Result: A value was modified by main Startmain end, a=10*/intMainintargcChar*argv[])        {pthread_t T1, T2, T3; intret, I; printf ("Main start\n"); RET= Pthread_create (&t1, NULL, THREAD1, NULL);        Pthread_cancel (t1);        Pthread_join (t1, NULL); Sleep (3); printf ("main end, a=%d\n", a); return 0;}/*Run Result: At the end of the program at the cancel point, the a value is not. Main Startmain end, a=0*/

. Canceling a thread

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.