1

My script has an asynchronous conversation that polls a queue for new messages, while the user performs other tasks. I've put the web_reg_async_attributes() into init, my callbacks are in asynccallbacks.c, and my main logic is in action.c The async polls every 5s, checking the message queue. When there is a message, I would like the callback to set a flag that the action.c has access to so that it can execute logic conditionally. I've tried using a global variable, declared in init, but it is not visible in asynccallbacks.c.

Is there a way to accomplish this? (I don't want to use files because I'm measuring activities that take less than a second and if I put the file system into the picture, my response times won't be representative).

Buzzy
  • 2,905
  • 3
  • 22
  • 31
Ned Parker
  • 23
  • 4

1 Answers1

1

In the first file (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;
}

In the second file (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;
}

In this example, the variable Global_Variable is defined in asynccallbacks.h. In order to utilize the same variable in action.h, it must be declared. Regardless of the number of files, a global variable is only defined once; however, it must be declared in any file outside of the one containing the definition.

  • Thanks all, these responses helped. My LR and C knowledge is obviously very rusty so I'll need to brush up. – Ned Parker Feb 18 '19 at 15:23