标签:style blog color io ar sp div art on
#include <stdlib.h> #include <pthread.h> #include <stdio.h> #include <sched.h> int a = 0; void *thread1(void *arg) { // pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); //如果把这行代码加上 a的值会改变的 a = 10; printf("start thread (%u)\n", (unsigned)pthread_self()); } int main(int argc, char *argv[]) { pthread_t t1, t2, t3; int ret, i; printf("main start\n"); ret = pthread_create(&t1, NULL, thread1, NULL); if(ret != 0) { printf("create thread failed\n"); exit(1); } pthread_cancel(t1); pthread_join(t1, NULL); sleep(3); printf("main end, a=%d\n", a); return 0; } /*运行结果: 设置为异步模式,在没到达取消点之前就结束了,a没被改动。如果把13行注释,a会被改动。 main start main end, a=0*/
标签:style blog color io ar sp div art on
原文地址:http://www.cnblogs.com/leijiangtao/p/3995823.html