I'm working on a game engine that runs from a .dll. Inside, there is an exported function that returns a reference to a static class declared in it, like below:
__forceinline __declspec(dllexport) STATE* NF3DGetEngineState(void)
{
static STATE s_State;
return &s_State;
}
where STATE
is a class that manages all components and has functions that accesses them via a critical section:
void Set(int val)
{
EnterCriticalSection(&CriticalSection);
ClassMember = val;
LeaveCriticalSection(&CriticalSection);
}
where "CriticalSection" is a CRITICAL_SECTION
member of the STATE
class that is of course initialised. The context in which I use these functions is:
NF3DGetEngineState()->Set(10);
The question is: is this code thread safe?
From what I learnt, returning references to static declarations is not thread safe.
What can I do to make it so?