如何用软件中断C中的运行功能? [英] How to software interrupt a running function in C?

查看:64
本文介绍了如何用软件中断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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆