2

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.

4

3 回答 3

1

通常,如果您需要执行此类操作,您将拥有一个可以处理多个任务的 OS/RTOS(即使操作系统是简单的自制类型的东西)。

如果您还没有 RTOS,您可能想研究一个(有数百个可用)或考虑合并一些简单的东西,如 protothreads:http ://www.sics.se/~adam/pt/

于 2009-04-10T19:33:00.593 回答
1

所以你有两个线程:一个运行内核,一个运行应用程序?您需要在内核中创建一个函数,说 ReadyToYield(),应用程序可以在您愿意关闭它时调用它。ReadyToYield() 将标记内核线程以向其提供好消息,然后坐下来等待内核线程决定要做什么。它可能看起来像这样:

volatile bool appWaitingOnKernel = false;
volatile bool continueWaitingForKernel;

在应用程序线程调用上:

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

在内核线程调用上:

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

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

显然,这里的实际实现取决于底层的 O/S,但这是要点。

约翰。

于 2009-04-10T19:36:20.973 回答
0

(1) 你需要编写线程安全的代码。这并不特定于嵌入式系统。

(2) 进行上下文切换时需要将状态保存起来。

于 2009-04-10T19:49:38.820 回答