Optimize the LowLevelPhysics2D API with multithreading
To improve the performance of 2D physics, run the simulation and your own code on multiple threads.
Read time 2 minutesLast updated 12 days ago
To improve the performance of the API, use your available CPU cores to run the physics simulation and your own 2D physics code on multiple threads.
LowLevelPhysics2DRun the simulation on multiple threads
To increase the number of worker threads the 2D physics system uses to calculate the simulation, set the Simulation Workers property after you create a world. The number of CPU cores on your device might mean the system uses fewer threads than you request.
To avoid performance issues, avoid changing the number of simulation workers at runtime.
Run your own code on multiple threads
To run API methods on all available CPU cores, use the job system to split the calculations across worker threads. For example, use multiple threads to check for overlaps between a very large number of physics objects.
LowLevelPhysics2DMost of the API is thread-safe, which means multiple worker threads can read and write the same physics data without interfering with each other. Inside a physics world, any number of threads can read data simultaneously, but only one thread can write data at a time. This is called a Write Once, Read Many (WORM) locking mechanism.
Each physics world is isolated from the others, so you can write data to different physics worlds on separate threads simultaneously.
The following methods aren't guaranteed to be thread-safe, so you can't use them in the job system:
- , for example
CreatePhysicsBody.Create - , for example
CreateBatchPhysicsBody.CreateBatch - , for example
DestroyPhysicsBody.Destroy - , for example
DestroyBatchPhysicsBody.DestroyBatch
For more information about creating a multithreaded job, refer to Create and run a job.