First question:
- Do threads need their own copies of variables?
- or do they need to coordinate access to one shared copy?
If you need the first one, other answers have made suggestions about "streaming local storage."
If you need the latter, somehow you need to provide the appropriate mutex for these variables (the size of the mutex is one of the problems you are facing), and that all threads use the mutex and free the mutex. This is harder. Perhaps even you need to provide functions that control access to variables.
The errno standard variable can be a variable:
extern int *_errno_func(void); #define errno (*(_errno_func)())
In a multi-threaded application (compiled with -DREENTRANT) this happens; on MacOS X, this is similar to what happens anyway (they use the name __error instead of _errno_func , both are in the implementation namespace).
You may want to or do something similar for your variables. The fact that you say that they are static improves the situation a bit. You have only one file to consider (unless you are too careful to pass back or pointers to these variables).
source share