多线程之线程的基本控制
一,掌握线程的终止方式,线程的连接,退出操作,清理操作。 二,线程的清理操作是如何进行的? 【注】查看标准函数库的定义解释命令,如:man 3 exit 1,线程终止 1)exit是危险的: 若进程中任意一个线程调用了exit(),_Exit(),_exit(),那么整个进程就会终止。 2)不终止进程的退出方式: 3)例子1;验证几种退出方式 建立文件test_several_exits.c;在main函数调用sleep,睡眠2秒确保子线程先运行完,再运行主线程main;内容如下: #include"unistd.h" #include"sys/types.h" #include"pthread.h" #include"stdlib.h" #include"string.h" void *pthread_fun(void *arg) { if(strcmp(arg,"1")==0) { printf("new return by return\n"); return (void*)1; } if(strcmp(arg,"2")==0) { printf("new return by pthread_exit\n"); pthread_exit( (void*)2); } if(strcmp(arg,"3")==0) { printf("new return by exit\n");