Original Post
I've done a few school projects with threads so I have a good idea about mutexes and semaphores.
I sometimes see the phrase, "this code is thread safe" Are there ways to make code threadsafe besides using mutexes around critical sections?
I was reading a bit about STL for example.
Is STL stuff threadsafe?
Reading is threadsafe, but if a write is happening, only that thread should have access. That makes sense. So how do you lock something like that? How would my writing thread try to get a lock on some data when there are 3 other threads reading from it? Do I just use a semaphore and try to lock the semaphore 3 times so I know no threads are reading from it?
My plan is to rewrite my game engine to use threads for different processes.
I'd have the game logic loop and I think PhysX on a separate loop although I'm not sure how physics can run asynchronously from the rest of the game.
Then I'd have the sound thread.
A networking thread when it comes to that...
The graphics thread.
PhysX 3 also comes with a thread pool system for random tasks that I can can probably use for rendering and whatever else might work.
Things like sound and graphics would be reading from data written by the game logic thread. I'd need a to lock the data whenever the game logic updates. The PhysX manual says it keeps a double buffer of data so threads can read while PhysX is doing a write. I guess I could do that with my game logic too, which could get to be a lot of data. I'm not really sure if that's the best way.
I of course want to avoid having too many locks all over the place or else I won't even be getting any advantage from threading.
(Ah man writing a game engine is hard when you try to make things perfect)
I sometimes see the phrase, "this code is thread safe" Are there ways to make code threadsafe besides using mutexes around critical sections?
I was reading a bit about STL for example.
Is STL stuff threadsafe?
Reading is threadsafe, but if a write is happening, only that thread should have access. That makes sense. So how do you lock something like that? How would my writing thread try to get a lock on some data when there are 3 other threads reading from it? Do I just use a semaphore and try to lock the semaphore 3 times so I know no threads are reading from it?
My plan is to rewrite my game engine to use threads for different processes.
I'd have the game logic loop and I think PhysX on a separate loop although I'm not sure how physics can run asynchronously from the rest of the game.
Then I'd have the sound thread.
A networking thread when it comes to that...
The graphics thread.
PhysX 3 also comes with a thread pool system for random tasks that I can can probably use for rendering and whatever else might work.
Things like sound and graphics would be reading from data written by the game logic thread. I'd need a to lock the data whenever the game logic updates. The PhysX manual says it keeps a double buffer of data so threads can read while PhysX is doing a write. I guess I could do that with my game logic too, which could get to be a lot of data. I'm not really sure if that's the best way.
I of course want to avoid having too many locks all over the place or else I won't even be getting any advantage from threading.
(Ah man writing a game engine is hard when you try to make things perfect)