如何用软件中断C中的运行功能? [英] How to software interrupt a running function in C?
问题描述
我想在函数运行时生成中断,然后希望CPU执行某些特定的ISR,完成ISR后,该函数将恢复执行.我正在使用Windows XP和MinGW编译器.
I want to generate an interrupt while a function is running and then I want the CPU to execute some specific ISR, and after finishing the ISR, the function will resume executing. I am using Windows XP and the MinGW compiler.
推荐答案
实际上没有硬件,我想首先以某种方式生成中断,然后稍后再处理,我将在实际硬件上进行处理
no hardware is there actually I want first to generate interrupt somehow and handle it later on I will do it on actual hardware
真正的硬件中断将切换CPU引脚之一,并将其分配给ISR.我想不出任何真正透明的方式来模仿它.
A real hardware interrupt will toggle one of the CPU pins and cause it to dispatch to the ISR. I can't think of any really transparent way to emulate that.
One way to pause the currently-running function might be to use the SuspendThread function, and then use a different thread to run the ISR.
A different possibility to investigate might be to use Fibers.
修改
如 cxxl的答案所建议的那样,另一种可能性是使用实时优先级线程:
As cxxl's answer suggested, another possibility is to use a real-time priority thread:
- 具有两个线程,一个线程运行您的函数,另一个线程运行模拟的ISR
- 在两个线程上都使用"CPU亲和力"功能,以确保它们都在同一CPU上运行
- 将ISR线程的优先级设置为实时"
然后,每当允许运行ISR仿真线程时,它将抢占另一个线程.
Then, whenever you allow the ISR-emulating thread run, it will pre-empt the other thread.
这篇关于如何用软件中断C中的运行功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!