Due to performance requirements, we have moved some code logic to execute on a Worker Thread. However, we’ve encountered some ensures related to GetValueOnGameThread from CVar. Therefore, I would like to inquire about what needs to be considered if we want to change these instances of GetValueOnGameThread to GetValueOnAnyThread.
::GetValueOnAnyThread is slightly slower than ::GetValueOnGameThread or ::GetValueOnRenderThread as it will make some minor calls (::IsInParallelGameThread and ::IsInGameThread) to see if it should return the main thread version of the cvar or the shadowed version but unless it is being called inside of a tight for loop it is not likely to make a noticeable difference.
It’s also worth noting that the shadowed version of the cvar is only relevant if the cvar uses the ECVF_RenderThreadSafe flag. When a cvar with that flag is updated on the GameThread instead of updating the shadowed value immediately, that work will be queued up as a render command so that it will only be updated when the render thread gets to it.
In summary, go ahead and make the change to use ::GetValueOnAnyThread it will almost certainly not be a problem for you.
It occurred to me this morning that I forgot to mention that if the cvar is one of your own you could consider changing it to be a FAutoConsoleVariableRef which acts as a wrapper around a normal variable.
Example:
bool bValueThatCodeUses= true; static FAutoConsoleVariableRef CVar_ValueThatCodeUses ( TEXT("mysystem.MyValue"), bValueThatCodeUses, TEXT("An example of how to use FAutoConsoleVariableRef") ); ... void MyFunctionThatRunsOnAnyThread() { if (bValueThatCodeUses) { ... } }Any change to the cvar mysystem.MyValue is reflected to the global variable bValueThatCodeUses which can be used without risking any additional thread checking.