createthread和beginthread句柄数只增不降

来源:互联网 发布:海尔智能家居网络设置 编辑:程序博客网 时间:2024/04/27 20:47

由于中继服务器句柄增加,随带测试了创建线程句柄数量的问题

1  调用API :createthread,closehandle
   线程函数 :func{printf} 
   执行次数 :1
   结果:       句柄 为10
 
2  调用API :createthread,closehandle
   线程函数 :func{printf} 
   执行次数 :2,200,20000
   结果:       句柄 为10,11,12(相同执行次数,句柄也会不同,但在这三值浮动,为固定值)
 
 
3  调用API :createthread,closehandle
   线程函数 :func{}         //注这里空函数
   执行次数 :2,200,20000
   结果:       和2差不多
 
 
4  调用API :_beginthread
   线程函数 :func{} 
   执行次数 :1,2
   结果:       句柄都为 10
 
 
5  调用API :_beginthread
   线程函数 :func{} 
   执行次数 :20
   结果:       句柄 11
 
6 调用API :_beginthread
   线程函数 :func{} 
   执行次数 :20000
   结果:       句柄 13,14    

结果来看,就算closehandle或者它自己将引用计数减一,句柄数并一定会随着减少到相应值,而
是保持在一个范围.不过应该没啥关系~~
后记:在msdn看到_beginthread如下的话
It is safer to use _beginthreadex than _beginthread. If the thread spawned by _beginthread exits quickly, the handle returned to the caller of _beginthread may be invalid or, worse, point to another thread. However, the handle returned by _beginthreadex has to be closed by the caller of _beginthreadex, so it is guaranteed to be a valid handle if _beginthreadex did not return an error.

从这句话来看前面句柄数不稳定,或者会上升在于线程函数执行过快,返回的句柄有可能无效导致的