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

Source: Internet
Author: User

12.7 canceling a thread sometimes, you want a thread to be able to ask for a thread to terminate, just like sending a signal to it.

Threads have methods to do this, as with signal processing. A thread can change its behavior when it is requested to terminate.
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 be able to send a request to cancel it.
Threads can use 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 ignores the cancellation request. The oldstate pointer is used to get the previous cancellation state.
Assuming the cancellation request is accepted, the thread is able to enter the second control level, using 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 receiving the cancellation request, and the other is pthread_cancel_deferred, which will make it possible to accept the cancellation request. Wait until the thread runs one of the following functions to take action. Verbose is the function pthread_join,pthread_cond_wait,pthread_cond_timedwait,pthread_testcancel,sem_wait or sigwait.
Code 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 is required to set the cancellation state and the cancellation type of the fresh thread, respectively ******************************************************************* /#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's parameters are 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 waitThread A_thread is merged with the main thread again */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 the consent 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 cancellation type of the thread, pthread_cancel_deferred will make the cancellation request after receiving. Wait until the thread runs 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 running) and then sends a cancellation request. For example, see the following:
Sleep (3);p rintf ("canceling thread...\n"); res = Pthread_cancel (a_thread);
In the newly created thread. First set the cancellation status to agree to cancel, as seen in the following:
res = Pthread_setcancelstate (pthread_cancel_enable, NULL);
Then set the cancellation type to deferred cancellation, as seen in the following:
res = Pthread_setcanceltype (pthread_cancel_deferred, NULL);
At last. The thread waits in the loop to be canceled, as seen in the following:
for (i = 0; i < i++) {    printf ("Thread is still running (%d) ... \ n", i);    Sleep (1);}


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.