wince driver中,与中断关联过的EVENT,在IST线程中使用WaitForMultipleObjects fail出错

在WinCE驱动开发过程中,尝试使用WaitForMultipleObjects函数在一个中断服务线程(IST)中处理两个硬件中断,但遇到错误87。经过查阅MS帮助文档,发现与中断关联过的EVENT不能如此使用。为防止遗忘并帮助遇到相同问题的开发者,作者分享了这一经验教训。
摘要由CSDN通过智能技术生成

        最近在做wince的驱动中,想在一个IST线程中处理两个硬件中断,自己想偷懒,不想用两个线程来做,故就用WaitForMultipleObjects函数去等待那两个中断线程,一直不能成功。用GetLastError(),获取msg全是87,说是传参数有问题,但是我两个事件的创建都是成功的啊,没有相应的error的log信息。郁闷了一个下午,后来在查了帮助ms的帮助文档,终于找到原因了,我不能那样干。

 

自己记性不好,故记录在这里,以防自己忘记。同时希望,能对和我犯同样错误的看客有所帮助。呵呵

本文链接地址:http://blog.csdn.net/bbw2008/archive/2011/05/04/6393329.aspx

 

与中断关联过的EVENT,在IST线程中,只能使用

DWORD WaitForSingleObject(
  HANDLE hHandle,
  DWORD dwMilliseconds
);
不能使用
DWORD WaitForMultipleObjects(
  DWORD nCount,
  CONST HANDLE* lpHandles,
  BOOL fWaitAll,
  DWORD dwMilliseconds
);
详细wince的InterruptInitialize函数的帮助文档,
ms-help://MS.VSCC.v80/MS.VSIPCC.v80/MS.WindowsCE.v60.en/CE_OS_IntExplorer/html/4ae3a36b-759a-4103-8251-ecac4c440103.htm

This function must be called before using the hEvent parameter, which provides a link between the idInt parameter and the SYSINTR value returned by an ISR.

The hEvent parameter can be used only in a WaitForSingleObject call to wait for the event to be triggered by the kernel.

A WaitForMultipleObjects call with hEvent fails.

If you use hEvent in a call to WaitForSingleObject before you call InterruptInitialize, InterruptInitialize fails.

InterruptInitialize is a kernel-mode-only function. For more information about kernel-mode-only functions, see Kernel Mode APIs.

InterruptInitialize is used with device drivers and user-mode drivers. For more information about user-mode drivers, see User Mode Driver Framework.

`WaitForMultipleObjects` 函数可以用于等待多个对象(如线程、事件、互斥体等)的任意一个对象被信号激发。该函数会阻塞当前线程直到有一个对象被激发或者超时。 以下是一个使用 `WaitForMultipleObjects` 函数等待多个线程执行完毕的示例代码: ```c #include <windows.h> #include <stdio.h> #define THREAD_COUNT 3 DWORD WINAPI thread_func(LPVOID lpParam) { int id = (int)lpParam; printf("Thread %d started\n", id); // 模拟耗时操作 for (int i = 0; i < 1000000000; i++); printf("Thread %d finished\n", id); return 0; } int main() { HANDLE hThreads[THREAD_COUNT]; for (int i = 0; i < THREAD_COUNT; i++) { hThreads[i] = CreateThread(NULL, 0, thread_func, (LPVOID)i, 0, NULL); if (hThreads[i] == NULL) { printf("Failed to create thread %d\n", i); return 1; } } DWORD waitResult = WaitForMultipleObjects(THREAD_COUNT, hThreads, TRUE, INFINITE); if (waitResult == WAIT_FAILED) { printf("WaitForMultipleObjects failed\n"); return 1; } printf("All threads finished\n"); for (int i = 0; i < THREAD_COUNT; i++) { CloseHandle(hThreads[i]); } return 0; } ``` 这段代码创建了 3 个线程,并使用 `WaitForMultipleObjects` 函数等待所有线程执行完毕。其第一个参数是对象个数,第二个参数是对象数组,第三个参数指定是否等待所有对象都被激发,第四个参数指定超时时间。在本例,第三个参数为 `TRUE` 表示等待所有对象都被激发。当等待函数返回时,可以判断返回值以确定哪个对象被激发。需要注意的是,等待函数返回时需要关闭对象句柄,否则可能会导致内存泄漏。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值