Linux pthread_cancel cannot cancel the thread's cause "go"

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/huangshanchun/article/details/47420961

Copyright NOTICE: Welcome to reprint, if there are deficiencies, please treatise.

One thread can call Pthread_cancel to terminate another thread in the same process, but it is worth emphasizing that, between threads in the same process, pthread_cancel terminates the signal to another Cheng. The system does not close the canceled thread immediately, and only the thread will actually end when the next system call is canceled. or call Pthread_testcancel to let the kernel detect if the current thread needs to be canceled. The canceled thread, the exit value, defines the value of the constant pthread_canceled in the Linux pthread Library is-1.

[CPP]View PlainCopy
    1. #include <pthread.h>
    2. int pthread_cancel (pthread_t thread);


See the following procedure:

[CPP]View PlainCopy
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4. void *thread_fun (void *arg)
  5. {
  6. int i=1;
  7. printf ("thread start \ n");
  8. While (1)
  9. {
  10. i++;
  11. }
  12. return (void *) 0;
  13. }
  14. int main ()
  15. {
  16. void *ret=null;
  17. int iret=0;
  18. pthread_t Tid;
  19. Pthread_create (&tid,null,thread_fun,null);
  20. Sleep (1);
  21. Pthread_cancel (TID); //Cancel thread
  22. Pthread_join (Tid, &ret);
  23. printf ("Thread 3 exit code%d\n", (int) ret);
  24. return 0;
  25. }


Will find that the program continues to run, the thread can not be canceled, the reason Pthread_cancel to another line Cheng stop signal. The system does not close the canceled thread immediately, and only the thread will actually end when the next system call is canceled. If the thread does not perform a system call, it can be resolved using Pthread_testcancel.

[CPP]View PlainCopy
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4. void *thread_fun (void *arg)
  5. {
  6. int i=1;
  7. printf ("thread start \ n");
  8. While (1)
  9. {
  10. i++;
  11. Pthread_testcancel ();
  12. }
  13. return (void *) 0;
  14. }
  15. int main ()
  16. {
  17. void *ret=null;
  18. int iret=0;
  19. pthread_t Tid;
  20. Pthread_create (&tid,null,thread_fun,null);
  21. Sleep (1);
  22. Pthread_cancel (TID); //Cancel thread
  23. Pthread_join (Tid, &ret);
  24. printf ("Thread 3 exit code%d\n", (int) ret);
  25. return 0;
  26. }


Linux pthread_cancel cannot cancel the thread's cause "go"

Related Article

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.