再谈线程等待函数WaitForSingleObject和CloseHandle
来源:互联网 发布:域名访问升级中 编辑:程序博客网 时间:2024/05/19 06:46
多线程中使用WaitForSingleObject结束线程,啥也不说,先上代码
#include <windows.h>#include <String.h>#include <stdio.h>int Fun1Proc( void ){int i;char *c = "thread one " ; for(i = 0; i < 100; i++){printf("%s number: %d \n",c ,i+1 );}return 0;}int main( ){HANDLE hThread1;hThread1 = CreateThread(NULL, 0, Fun1Proc, NULL, 0, NULL);int i;char *c = "main thread " ; for(i = 0; i < 100; i++){printf("%s number: %d \n",c ,i+1 );}CloseHandle(hThread1);return 0;}执行结果:
main thread 1
main thread 2
main thread 3
...
main thread 98
main thread 99
main thread 100
为什么线程函数没有执行呢?奇怪,多运行几遍,发现每次结果不一定相同,下面是另一次执行结果:
main thread 1
main thread 2
main thread 3
...
main thread 71
thread one 1
thread one 2
main thread 72
main thread 73
...
thread one 3
thread one 4
main thread 75
main thread 76
...
main thread 98
main thread 99
main thread 100
可见,主函数运行完了,整个程序就退出了,不会等到Fun1Proc()运行完再退出。等2秒会不会好一点呢?下面是修改后的main程序
int main( ){HANDLE hThread1;hThread1 = CreateThread(NULL, 0, Fun1Proc, NULL, 0, NULL);Sleep(2000);int i;char *c = "main thread " ; for(i = 0; i < 100; i++){printf("%s number: %d \n",c ,i+1 );}return 0;}
运行结果:
thread one 1
thread one 2
thread one 3
...
thread one 98
thread one 99
thread one 100
main thread 1
main thread 2
main thread 3
...
main thread 98
main thread 99
main thread 100
int main( ){HANDLE hThread1;hThread1 = CreateThread(NULL, 0, Fun1Proc, NULL, 0, NULL);WaitForSingleObject(hThread1, INFINITE);int i;char *c = "main thread " ; for(i = 0; i < 100; i++){printf("%s number: %d \n",c ,i+1 );}CloseHandle(hThread1);return 0;}这下好了,每次运行结果都正确,WaitForSingleObject的作用就是等待线程自动结束,继续执行后面的程序。这个CloseHandle为什么这么靠后呢?把它放前面一点好不好?不是说每次创建线程以后直接CloseHandle更好吗?好,那就挪到上面试试
int main( ){HANDLE hThread1;hThread1 = CreateThread(NULL, 0, Fun1Proc, NULL, 0, NULL);CloseHandle(hThread1);WaitForSingleObject(hThread1, INFINITE);int i;char *c = "main thread " ; for(i = 0; i < 100; i++){printf("%s number: %d \n",c ,i+1 );}return 0;}
运行后发现,又和一开始一样了,main thread 中穿插着 thread one。 原来是CloseHandle之后,WaitForSingleObject得不到thread的句柄,没法再等到这个thread结束,呵呵
0 0
- 再谈线程等待函数WaitForSingleObject和CloseHandle
- 线程的CloseHandle和WaitForSingleObject
- 线程等待WaitXXXXX()函数系列之WaitForSingleObject和WaitForMultipleObject
- WaitForSingleObject 和 WaitForMultipleObjects函数 (让线程挂起等待事件)
- 线程同步 等待函数 WaitForSingleObject WaitForMultipleObjects
- 等待线程退出 MsgWaitForMultipleObjects和WaitForSingleObject
- WaitForSingleObject 等待线程句柄
- WaitForSingleObject 等待线程句柄
- 【VS开发】WaitForSingleObject 和 WaitForMultipleObjects函数 (让线程挂起等待事件)
- 主线程使用WaitForSingleObject和MsgWaitForMultipleObjects等待线程返回值
- WaitForSingleObject等待退出线程bug
- 线程中的CloseHandle()函数
- (CloseHandle函数)内核对象、进程和线程
- 等待线程结束的高手--WaitForSingleObject
- Windows和Linux上分别怎样实现等待某个线程结束?---用WaitForSingleObject和pthread_join
- 多线程编程(9) - 认识等待函数 WaitForSingleObject
- 多线程编程(9) - 认识等待函数 WaitForSingleObject
- 多线程编程(9) - 认识等待函数 WaitForSingleObject
- 前端,财务数据的正则表达式(一篇专门写给自己的博客)
- 【C语言】单链表相关面试题(二)
- ContextLoaderListener与DispatcherServlet
- 指针与引用的区别
- Android平台对H264视频硬解码
- 再谈线程等待函数WaitForSingleObject和CloseHandle
- oracle 10g for linux
- Weblogic服务器启动异常:unable to validate weblogic domain
- Ui listview 类
- Linux gcc 编译
- 80端口被system占用的问题(占用PID:4 System)
- FreeRTOS 任务管理之任务删除
- 学习路线
- 如何将JS的变量传递给highcharts中series:data,感觉都是基于JS,怎么就不行呢?