Let's say I'm building a library to spork quuxes in C.
Quuxes need two state variables to be sporked successfully:
static int quux_state;
static char* quux_address;
/* function to spork quuxes found in a file,
reads a line from the file each time it's called. */
void spork_quux(FILE*);
If I store that data as global variables, only a single client will be able to spork quuxes at a single time, else the state variables will get mangled by a second caller and disaster may ensue.
The question is what's the best way to design a reentrant library in C?
I've entertained the following cases, to no satisfactory conclusion.
In the following case the question is how to associate a client to each state?
/* library handles all state data allocation */
static int* quux_state;
static char** quux_address;
In the following case the client is able to mess with the state, very undesirable
/* let each client store state */
typedef struct { int state; char* address; } QuuxState;
QuuxState spork_quux(FILE*);
So, how to do this properly?