Windows线程中调用WriteFileEx没有触发完成函数回调

近期为了封装部分功能模块,将Read和Write的逻辑封装到另一个线程中处理时发现,WriteFileEx没有触发lpCompletionRoutine。CreateFile的时候确定设置了FILE_FLAG_OVERLAPPED。

查看文档发现,要触发完成实例需要保证调用WriteFileEx的线程是alterable状态。如果线程使用sleep等待结束,WriteFileEx是无法完成回调的,但是如果调用SleepEx就可以触发。

根据MSDN对WriteFileEx的lpCompletionRoutine的讲解

A pointer to a completion routine to be called when the write operation has been completed and the calling thread is in an alertable wait state. For more information about this completion routine, see FileIOCompletionRoutine.

调用WriteFileEx的线程必须是alertable的。

MSDN中搜索alertable io

Alertable I/O - Win32 apps | Microsoft Learn

里面描述了FileIOCompletionRoutine中提及的SleepEx, MsgWaitForMultipleObjectsEx, WaitForSingleObjectEx, or WaitForMultipleObjectsEx。使用Ex函数所在的线程是alterable。否则无法触发完成回调。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值