如何在 asynccallbacks.c 和 action.c 之间共享变量

How to share variables between asynccallbacks.c and action.c

我的脚本有一个异步会话,可以在用户执行其他任务的同时轮询队列中的新消息。我已经将 web_reg_async_attributes() 放入 init,我的回调在 asynccallbacks.c,我的主要逻辑在 action.c 异步每 5 秒轮询一次,检查消息队列。当有消息时,我希望回调设置 action.c 可以访问的标志,以便它可以有条件地执行逻辑。我试过使用在 init 中声明的全局变量,但它在 asynccallbacks.c.

中不可见

有没有办法做到这一点? (我不想使用文件,因为我正在测量不到一秒的活动,如果我将文件系统放入图片中,我的响应时间将不具有代表性)。

在第一个文件中(asynccallbacks.h):

// Explicit definition, this actually allocates
// as well as describing
int Global_Variable;

// Function prototype (declaration), assumes 
// defined elsewhere, normally from include file.       
void SomeFunction(void);        

int main(void) {
    Global_Variable = 1;
    SomeFunction();
    return 0;
}

在第二个文件中(action.c):

// Implicit declaration, this only describes and
// assumes allocated elsewhere, normally from include
extern int Global_Variable;  

// Function header (definition)
void SomeFunction(void) {       
    ++Global_Variable;
}

在这个例子中,变量Global_Variable定义在asynccallbacks.h中。为了在 action.h 中使用相同的变量,必须对其进行声明。不管有多少个文件,一个全局变量只定义一次;但是,它必须在包含定义的文件之外的任何文件中声明。