So here is the problem I'm trying to solve, I'm programming in C.
We have a function that can initialize a struct for you.
typedef struct {
int val1;
int val2;
} custom_t;
custom_t init_custom() {
custom_t temp;
temp.val1 = 5;
temp.val2 = 5;
return temp;
}
And you would just use it like so:
custom_t some_name = init_custom();
I have 4 functions that takes the custom_t as input and can do some work with it.
In another file I have a lot of library functions that will run in a multithreaded enviroment. These library functions will all need to do work on the same custom_t variable, no matter the thread.
The library functions won't get the custom_t variable passed to it, because the goal is that another user should be able to use the library functions without thinking of the custom_t variable.
I'm thinking that I have to make the custom_t variable global in the namespace where I define the library functions but I an error saying that global variables must be const.
I am unsure how to achieve this and I would appreciate all the help I can get. If my explanation wasn't good enough feel free to ask any questions and I will try to elaborate.
EDIT: Fixed the variable init typo
With
custom_t = init_custom();
you're trying to set a typename (i.e.custom_t
).Just call it something else:
But, to access this from multiple threads and library functions, assuming you'll need to write to it, you'll need to wrap access to this in a mutex:
UPDATE:
My example wasn't intended to be literally an initializer but an assignment:
UPDATE #2:
Another way [under
gcc
at least], is to create a contructor function. Given the above functions and definitions, move the init calls into:Nothing needs to [and nothing should] call this function. It will be called automatically before
main
is called because it's now a special function.These are often used by libraries that want to do some init, but don't want to burden
main
with having to know to call (e.g.)init_liba(); init_libb(); ...
In this case, it is called at the "right" time for the library [based upon linkage, etc.].There is also a
__attribute__((destructor))
than can be used to "destroy" things [aftermain
returns, IIRC].For more on this, see: How exactly does __attribute__((constructor)) work?
Personally, I now use the above attribute, but, for nostalgia, I like the older
.init/.fini
sections.