hyh@hyh-OptiPlex-9020:~/git/hyh_home/1512_home/应用程序设计/多线程$ cat th.c
#include <pthread.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#define handle_error_en(en, msg) \
do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
static void *
thread_func(void *ignored_argument)
{
int s;
/* Disable cancellation for a while, so that we don't
immediately react to a cancellation request */
s = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
if (s != 0)
handle_error_en(s, "pthread_setcancelstate");
printf("thread_func(): started; cancellation disabled\n");
sleep(5);
printf("thread_func(): about to enable cancellation\n");
s = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
if (s != 0)
handle_error_en(s, "pthread_setcancelstate");
/* sleep() is a cancellation point */
sleep(1000); /* Should get canceled while we sleep */
/* Should never get here */
printf("thread_func(): not canceled!\n");
return NULL;
}
int
main(void)
{
pthread_t thr;
void *res;
int s;
/* Start a thread and then send it a cancellation request */
s = pthread_create(&thr, NULL, &thread_func, NULL);
if (s != 0)
handle_error_en(s, "pthread_create");
sleep(2); /* Give thread a chance to get started */
printf("main(): sending cancellation request: thr:%d\n",thr);
s = pthread_cancel(thr);
if (s != 0)
handle_error_en(s, "pthread_cancel");
/* Join with thread to see what its exit status was */
printf("pthread_cancel call\n");
s = pthread_join(thr, &res); //等待thr 被取消
if (s != 0)
handle_error_en(s, "pthread_join");
if (res == PTHREAD_CANCELED)
printf("main(): thread was canceled\n");
else
printf("main(): thread wasn't canceled (shouldn't happen!)\n");
exit(EXIT_SUCCESS);
}
hyh@hyh-OptiPlex-9020:~/git/hyh_home/1512_home/应用程序设计/多线程$ ./a.out
thread_func(): started; cancellation disabled 知道状态变为able,会被取消。
main(): sending cancellation request: thr:-1309870336
pthread_cancel call main:线程运行到这里,等待子线程退出.
thread_func(): about to enable cancellation
main(): thread was canceled
hyh@hyh-OptiPlex-9020:~/git/hyh_home/1512_home/应用程序设计/多线程$
转载于:https://my.oschina.net/tplinuxhyh/blog/542313