C++ threading performance
18 hours ago, Tornation said:
Thank you guys, will check it out. Sorry for late reply.
Starting/killing threads is very expensive, doing it 60 times a seconds is not efficient.
That way of threading only works when you have a ton to do in those threads so that the overhead of starting/killing threads is low compared to the total execution time of the program.
All modern game engines use some kind of task system (often using a task graph).
For a simple engines like yours you could consider:
- start threads at start up
- have a thread safe queue from which all worker threads get work when they're not doing anything
- put tasks in the queue from the main thread
This is called the producer/consumer pattern.
Tasks would be structs containing a data pointer and a function pointer (which will get executed on the data).
Make sure that everything is thread safe and there are no race conditions.
More advanced systems let tasks spawn new tasks creating a graph.
One of the hardest parts is to make sure that graph gets executed as efficiently as possible.
There are a couple of good talks available from GDC vault for free:
http://www.gdcvault.com/play/1012321/Task-based-Multithreading-How-to (maybe a bit too old/outdated (2010), havent seen it myself)
http://www.gdcvault.com/play/1022106/Lessons-from-the-Core-Engine (destiny's multithreaded engine)
http://www.gdcvault.com/play/1021926/Destiny-s-Multithreaded-Rendering (more about destiny's multithreaded engine)
http://www.gdcvault.com/play/1022186/Parallelizing-the-Naughty-Dog-Engine (parallelizing the last of us engine for the PS4 remastered version, really good talk IMO)
Furthermore, I found this blog to be quite informative:

Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now