I have a logging thread and a main app thread.
The main app runs every 50ms and has an usual tick length of 11ms to do all its things it needs to do. This 11ms or however long the tick lasted is logged in a variable in the main app in a public field.
In my logging thread i'd like to monitor the variable that keeps track of the average tick length in the main app thread to know when I should throttle the logging because main app flow will always have priority over logging. It is a public variable in the main app, unfortunately I have no control over the design of the main app and cannot make it a "thread safe" variable.
However I only want to read it and not write to it.
Can I safely read the variable directly(It doesn't really matter if the data is out of sync for a few microseconds or that i'm running behind on the data) or should I make a thread safe method that reads the variable for me and inject that in the main app via reflection?
What are the possible issues I should keep mind of? And what would be the best approach?
The intent of this question is to get an idea of all issues that could pop up in this scenario and what I should be mindful of. I'm not interested in a patch code solution, but more interested in learning the behaviour of threads and variables between threads and what pitfalls can be..