我的脚本有一个异步会话,用于轮询队列中的新消息,同时用户执行其他任务。我把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
中使用相同的变量,必须声明它。无论文件数量多少,全局变量只定义一次;但是,它必须在包含定义的文件之外的任何文件中声明。