Say I'm creating a library to handle threads in C.
Two state variables are required for tidying up:
static int quux_state; static char* quux_address; void spork_quux(FILE*);
If I store this data as global variables, only one client will be able to start threads at one time, otherwise the state variables will be distorted by the second caller and disaster can occur.
The question is, what is the best way to create a reentrant library in C?
I have considered the following cases so that there is no satisfactory conclusion.
In the following case, the question arises, how to associate the client with each state?
static int* quux_state; static char** quux_address;
In the following case, the client can ruin the state, it is very undesirable
typedef struct { int state; char* address; } QuuxState; QuuxState spork_quux(FILE*);
So how to do it right?
c design encapsulation reentrant
Vinko vrsalovic
source share