在 C++ 中處理中斷 (Handling Interrupt in C++)


問題描述

在 C++ 中處理中斷 (Handling Interrupt in C++)

I am writing a framework for an embedded device which has the ability to run multiple applications.  When switching between apps how can I ensure that the state of my current application is cleaned up correctly?  For example, say I am running through an intensive loop in one application and a request is made to run a second app while that loop has not yet finished.  I cannot delete the object containing the loop until the loop has finished, yet I am unsure how to ensure the looping object is in a state ready to be deleted.  Do I need some kind of polling mechanism or event callback which notifies me when it has completed?

Thanks.


參考解法

方法 1:

Usually if you need to do this type of thing you'll have an OS/RTOS that can handle the multiple tasks (even if the OS is a simple homebrew type thing).

If you don't already have an RTOS, you may want to look into one (there are hundreds available) or look into incorporating something simple like protothreads: http://www.sics.se/~adam/pt/

方法 2:

So you have two threads: one running the kernel and one running the app? You will need to make a function in your kernel say ReadyToYield() that the application can call when it's happy for you to close it down. ReadyToYield() would flag the kernel thread to give it the good news and then sit and wait until the kernel thread decides what to do. It might look something like this:

volatile bool appWaitingOnKernel = false;
volatile bool continueWaitingForKernel;

On the app thread call:

void ReadyToYield(void)
{
    continueWaitingForKernel = true;
    appWaitingOnKernel = true;
    while(continueWaitingForKernel == true);
}

On the kernel thread call:

void CheckForWaitingApp(void)
{
    if(appWaitingOnKernel == true)
    {
        appWaitingOnKernel = false;

        if(needToDeleteApp)
            DeleteApp();
        else
            continueWaitingForKernel = false;
    }
}

Obviously, the actual implementation here depends on the underlying O/S but this is the gist.

John.

方法 3:

(1) You need to write thread-safe code. This is not specific to embedded systems.

(2) You need to save state away when you do a context switch.

(by ohnnyjMichael BurrJohn RichardsonPaul Nathan)

參考文件

  1. Handling Interrupt in C++ (CC BY-SA 3.0/4.0)

#interrupt #synchronization #C++






相關問題

無法在 java 命令行程序中捕獲中斷 (Unable to catch interrupt in java command line program)

不連續的 BitBlt 捕獲 (discontinuous BitBlt capture)

GPIO 更改狀態時如何更新 sysfs? (How is sysfs updated when a GPIO changes state?)

Linux IRQ 處理程序中的固有競爭條件 (Inherent race condition in Linux IRQ handlers)

STM32 外部中斷僅在調試模式下響應 (STM32 external interrupt responds only in debug mode)

Java中的線程 (Threading in Java)

如何在被 Python 殺死之前運行最後一個函數? (How to run one last function before getting killed in Python?)

在 C++ 中處理中斷 (Handling Interrupt in C++)

應用程序中斷瘋狂 (Application interrupts like crazy)

SetPriorityClass(REALTIME_PRIORITY_CLASS) 實際上做了什麼? (What does SetPriorityClass(REALTIME_PRIORITY_CLASS) actually do?)

如何找出軟件掛起的原因?(qemu + zephyr + tfm 的問題) (How to find out the cause of hanging software? (problem with qemu + zephyr + tfm))

將參數傳遞給系統調用 (Passing arguments to system calls)







留言討論