Jump to content

Thread safety of some API calls


photo

Recommended Posts

Hello,

Long time since this was discussed:

and we've also changed version from 2.5 to 2.11 (and later will to 2.13), so worth to check the status of the thread safety of some Unigine calls.

It seems that with 2.11 transition, many calls which previously were not to be called from background threads, now they are safe to do so. I have no currently not experience any issues in 2.13, but want to confirm that is safe to do the following calls in a background thread:

Node: setParent, setWorldTransform, setName
All Unigine::Ptr operations (like swap, =, referenced etc). All seems to have locks now.
All Unigine::*Ptr::create (mesh static, lights, texture etc)

Calls for nodes not yet inserted in the scene (no parent):
ObjectMeshStaticPtr: setCastShadow, setShadowMask, setViewportMask, getMaterialInherit etc.
ObjectMeshClusterPtr: setMesh
Unigine::LightOmniPtr calls like setShapeType, setShapeRadius etc.
Unigine::LightWorldPtr: setNumShadowCascades
Unigine::LightPtr: setEnabled, setIntensity, setVisibleDistance, setShadow etc.

Calls for things already in the scenegraph:
Object::setEnabled
MaterialPtr: setParameterFloat4, findParameter, setTexture

So let me know if some of the above may cause issues, unless I move them in the main thread (or lock/unlock around them).

Kind Regards,

Adrian L.

Link to comment

Hello Adrian,

Unigine::Ptr's reference counter is thread-safe now. There shouldn't be issues with passing Ptr to different threads. Instance Unigine::Ptr itself is not a thredsafe object. The same guarantees which std::shared_ptr has.

Mutating scene objects (transform, hierarchy) and rendering related classes (like Material) from a background thread is not recommended until the main loop is stopped. Because the scene can be in the process of rendering in that very moment. You can do some multithreading with stopping the main loop. WorldLogic::updateSyncThread provides that kind of parallelism.

Creation and mutation of not inserted in the scene objects is a gray zone. The engine and the editor do that but there's a risk that you'll run into some undiscovered issues. So usually we recommend to do all the work with nodes (and other main loop related objects) from the main-thread and achieve some parallelism with stopping the main loop.

We have an article with these principles described
https://developer.unigine.com/en/docs/2.12/code/fundamentals/thread_safety/?rlang=cpp

Link to comment
×
×
  • Create New...