Tying eg. the application update logic, scripts and physics into threading would be quite hard. Currently there’s a strictly sequential update, which I’ll illustrate with script functions but it would be the same with C++:
- Scene update (with deltatime dt)
-
- For each script object which uses it: Update(dt)
-
- Physics update, which is split into multiple fixed timestep updates if necessary
-
-
- For each script object which uses it: FixedUpdate(fixedDt)
-
-
- Update Bullet world with fixedDt
-
-
- For each script object which uses it: FixedPostUpdate(fixedDt)
-
- For each script object which uses it: PostUpdate(dt)
In this scenario, if the physics was on another thread, the fixed updates still couldn’t proceed before the physics step is ready. We could make varying timestep script update and physics update happen simultaneously, but that would lose the control to apply logic per physics step, which is often essential for things like character control.
I’m hesitant to do any multithreading which either introduces more latency into the frame overall, or makes programming the logic harder. The renderer could however be made to utilize threads better, for example preparation of multiple views could happen simultaneously on threads. I now have a development machine with a very beefy GPU but somewhat poor CPU and I see the framerate doubling when I disable occlusion in the water example (2 views), which certainly is not optimal.
Memory allocation happens with default new and delete, which are threadsafe. However note that the SharedPtr’s have no thread safety, so their reference counts aren’t safe to increase/decrease from multiple threads. It’s best to use raw pointers in all threaded work (this also assumes the objects being worked on won’t be born or killed during threaded updates)