I'm working on a simulation project using c++ on both windows and linux.
There will be several thousand objects (Perhaps 3000-5000) in the simulation. In plan to have several threads performing actions on the objects so that it makes good use of multi core machines, for example one thread handling movement and location of the objects, one (or more) handling interaction between the objects, one handling creation of new objects.
However this will require synchronization between the threads to ensure it works properly. So I was thinking that each object should contain a (pthread mutex / CRITICAL_SECTION) depending on the platform, and then each thread can lock the objects it currently working on. Because there are a lot of objects contention should be rare so it should be fast.
So my first question is are 3000-5000 pthread mutexes or windows critical sections too many? I don't know what limits both systems have. Or is there are better way to achieve this?
My second question is about data structures to hold the objects. Because objects can be created and "die" I feel the best way is to store a "list" of active objects that the work threads can iterate through. A c++ list is not thread safe though. If one of my threads deleted an object I would need to synchronize that with another object doing "next()" on the list. Also I'd need to ensure that the "next" object was locked before moving my iterator to it so I'd need some kind of global lock on the whole collection which I'd have to obtain before inserting / deleting objects but also before moving to the next object on a thread.
This seems rather painful and potentially slow. Is there are better way?