createthread和beginthread句柄数只增不降

本文通过实验对比分析了使用不同API创建线程句柄的数量变化,并探讨了句柄数量受线程函数执行速度的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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.

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值