用等待函数试下,并且设置事件例如:先创建事件(全局) hEvent = CreateEvent(...);void ThreadA_Proc(...) { WaitForSingleObject(hEvent, INFINITE);// 这里是无限等待,直到事件被线程B释放 }void ThreadB_Proc(...) { while(...) { // Reset... ResetEvent(hEvent); // B thread now need to do lots of thing ... // lots of things at here // Here's to notify A thread SetEvent(hEvent); } }
hEvent = CreateEvent(...);void ThreadA_Proc(...)
{
WaitForSingleObject(hEvent, INFINITE);// 这里是无限等待,直到事件被线程B释放
}void ThreadB_Proc(...)
{
while(...)
{
// Reset...
ResetEvent(hEvent); // B thread now need to do lots of thing
... // lots of things at here
// Here's to notify A thread
SetEvent(hEvent);
}
}
除非降低线程的优先级别
然后使用主线程的TIMER来控制执行,
不过效率会很低如果一定要很精确
使用FIBER钎程吧,自己来进行调度
有个奇怪的现象,打开media player 以后,sleep就精确了。
SuppressUnmanagedCodeSecurity
SuppressUnmanagedCodeSecurity
SuppressUnmanagedCodeSecurity
你查一下上面3个API的用法后,就可以实现精确计时。
1. windows不是实时操作系统。
2. Sleep(20)说明线程将自己放到阻塞队列当中,让操作系统在20ms后将其放入就绪队列当中
3. 但如果此时就绪的线程多于1个(一般是这样),它不一定会选中这个线程。
4. 软中断和硬件中断也做不到这点,因为cpu在做某些底层操作时可能关中断。
5. 你可以使用不可屏蔽中断来做,不过未免。。
不知楼主对精度要求有多高,下面是几种不同的替代方案。
1. 使用VxWorks这样的实时系统,这样可以达到微秒级。
2. 使用软中断也不错,关中断的时间毕竟不多,也不长。
3. 就是做个内核线程了(我曾经做过驱动程序,这是可取的)。
4. 最简单的是提高线程的优先级别,那么在就绪时更可能被调度,当然不是一定,他很难竞争过内核线程。