Modifying data from multiple threads is just a matter of using proper atomic operations to ensure correct reads and writes (Memory barriers, CAS, etc.). This have nothing todo with using static or class whatsoever.
This reminds me of a discussion I had with a previous boss:
Me: "We should use C++ strings, because they're cleaner and safer"
Him: "No, we should stay with C strings/pointers/arrays, it's just a matter of using proper operations to modify them"
Encapsulating these accesses in one place lets you do it properly, once. Having it global is trusting yourself and everyone else who touches the code in future to do it properly every single time.
Yeah mean like this?:
class Profiler {
private:
static ProfilerEvent _events[2][MAX_PROFILER_EVENTS];
volatile u64 arrayIndex_eventIndex;
public:
static void RecordEvent(EventType eventType, char *guid) {
...
}
};
This type of encapsulation is actually fine, but for my game development i do, i still prefer the other way which is the total opposite when i am writing business applications.
Btw: You can do this with a struct defined as a global in the exact same way as well.
So i like this much better:
struct Profiler {
ProfilerEvent _events[2][MAX_PROFILER_EVENTS];
volatile u64 arrayIndex_eventIndex;
};
static Profiler *globalProfiler = nullptr;
static void ProfilerInit(Profiler *profiler) {
globalProfiler = profiler;
}
static void RecordEvent(ProfilerEventType eventType, char *guid) {
Assert(globalProfiler != nullptr);
...
}
When i need to share this i explicitly call ProfilerInit on the part which gets shared.
This way i totally know, when this thing is shared between both and only on a dll reload i must reset the pointer.
In the case i use a class i have no idea how this will be handled and when the memory will be allocated.
The virtual memory is not shared between the executables, isnt it? I simply dont know.
Also regarding that string example:
If i write a application which makes extreme heavy use of string manipulation, i may would use std::string but for game dev i still prefer char arrays.