标签:style blog color io ar sp div art c
#include <stdlib.h> #include <pthread.h> #include <stdio.h> #include <sched.h> void *thread1(void *arg) { printf("start thread (%u)\n", (unsigned)pthread_self()); printf("thread (%u) end\n", (unsigned)pthread_self()); } int main(int argc, char *argv[]) { pthread_t t1, t2, t3; int ret; printf("main start\n"); ret = pthread_create(&t1, NULL, thread1, NULL); if(ret != 0) { printf("create thread failed\n"); exit(1); } pthread_cancel(t1); sleep(5); printf("main end\n"); return 0; } /*运行结果: main start start thread (3076238224) main end 注意:子线程并没设置取消点,但是却被取消了,原因是printf函数包含了一个个取消点(应该在函数结尾),在取消点检测到取消请求时结束线程,第二个printf不会运行,*/
标签:style blog color io ar sp div art c
原文地址:http://www.cnblogs.com/leijiangtao/p/3995816.html