Linux programming--Canceling a thread (12th chapter)

Source: Internet
Author: User

12.7 canceling a thread sometimes, you want a thread to require another thread to terminate, just like sending a signal to it. Threads have methods that can do this, as with signal processing, where a thread can be asked to change its behavior when terminated.
Pthread_cancel is a function for requesting a thread to terminate
#inlude <pthread.h>int pthread_cancel (pthread_t thread);
This function provides a thread identifier to send a request to cancel it.
Threads can be used pthread_setcancelstate Setting the cancellation state of a thread
#include <pthread.h>int pthread_setcancelstate (int state, int *oldstate);
The value of the first parameter can be pthread_cancel_enable, which allows the thread to receive a cancellation request, or pthread_cancel_disable, which is the function of ignoring the cancellation request. The oldstate pointer is used to get the previous cancellation state.
If the cancellation request is accepted, the thread can enter a second control level, using the Pthread_setcanceltype Setting the cancellation type
#include <pthread.h>int pthread_setcanceltype (int type, int *oldtype);
The type parameter can have two values: one is pthread_cancel_asynchronour, it will take action immediately after the cancellation request is received, and the other is pthread_cancel_deferred, which will make it possible to accept the cancellation request Wait until the thread executes one of the following functions before taking action. Specifically, the function pthread_join,pthread_cond_wait,pthread_cond_timedwait,pthread_testcancel,sem_wait or sigwait.
Writing the program thread7.c, the main thread sends a cancellation request to the threads it creates.
/************************************************************************* > File name:thread7.c > The DESCRIPTION:THREAD7.C program sends a cancel request to the new thread it creates in the main thread > author:liubingbing > Created time:2015/7/7 9:58:40 > Oth The new thread's calling function in the ER:THREAD7.C program needs to set the cancellation state and the type of cancellation for each thread ******************************************************************* /#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <pthread.h>void * Thread_function (void *arg); int main () {int res;pthread_t a_thread;void *thread_result;/* pthread_create function creates a new thread, The new thread identifier is saved in A_thread, the function called by the new thread is thread_function, the function parameter is NULL */res = pthread_create (&a_thread, NULL, Thread_function, NULL); if (res! = 0) {perror ("Thread creation failed"); exit (exit_failure);} Sleep (3);p rintf ("canceling thread...\n");/* Pthread_cancel function Request Thread a_thread Terminate */res = Pthread_cancel (A_thread); if (res!) = 0) {perror ("Thread cancelation failed"); exit (exit_failure);} printf ("Waiting for Thread to finish...\n");/* Pthread_join waitThe thread a_thread is re-merged with the main thread */res = Pthread_join (A_thread, &thread_result), if (res! = 0) {perror ("thread join Failed"); exit ( Exit_failure);} Exit (exit_success);} void *thread_function (void *arg) {int I, res;/* pthread_setcancelstate function sets the cancellation state of the thread, Pthread_cancel_enable allows the thread to receive the cancellation request Pthread_cancel_disable ignores cancellation Request */res = Pthread_setcancelstate (pthread_cancel_enable, NULL); if (res! = 0) {perror (" Thread pthread_setcancelstate failed "); exit (exit_failure);} /* Pthread_setcanceltype function sets the type of cancellation for the thread, pthread_cancel_deferred will cause the cancellation request to wait until the thread executes a function, such as Pthread_join, before taking action. res = Pthread_setcanceltype (pthread_cancel_deferred, NULL), if (res! = 0) {perror ("Thread Pthread_setcanceltype failed") ; exit (exit_failure);} printf ("Thread_function is running\n"); for (i = 0; i <; i++) {printf ("thread is still running (%d) ... \ n", i); sleep (1) ;} Pthread_exit (0);}
After creating a new thread in the usual way, the main thread sleeps for a while (so that the new thread has time to start executing), and then sends a cancellation request as follows:
Sleep (3);p rintf ("canceling thread...\n"); res = Pthread_cancel (a_thread);
In the newly created thread, first set the cancellation status to allow cancellation, as follows:
res = Pthread_setcancelstate (pthread_cancel_enable, NULL);
Then set the cancellation type to deferred cancellation, as follows:
res = Pthread_setcanceltype (pthread_cancel_deferred, NULL);
Finally, the thread waits in the loop to be canceled, as follows:
for (i = 0; i < i++) {    printf ("Thread is still running (%d) ... \ n", i);    Sleep (1);}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Linux programming--Canceling a thread (12th chapter)

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.