Jump to content
  • Advertisement
Rannion

Lots of threads started with small work, bad or good design?

Recommended Posts

Hi, I am sending data to peers and those data need to be retreived from a scenegraph with a mutex to lock the data.

The process of gathering the data is taking a bit less than a ms. I'm starting the thread every time I want to gather the data. If I'm running at 60 fps, I'm starting the thread 60 times per second so is that a performance or design problem?

Would it be much better to have the thread always running and some kind of mechanism to ask him to perform the task whenever it's needed, so around 60 or 120fps?

Also, does starting a thread creates some memory alloc/dealloc and then produce on the long run some kind of fragmentation?

Thank you all.

Share this post


Link to post
Share on other sites
Advertisement

Creating a thread is rather expensive operation.  You would have to look at the disassembly but I'm fairly sure there are allocations involved on the OS side of things.  Maybe look into a job system where you dispatch off small jobs to a thread pool.  Here is a GDC video and overview of how they multithreaded the Destiny engine:

 

Share this post


Link to post
Share on other sites

Memory fragmentation is not really an issue, but creating and destroying threads that often might be a performance problem.  I'd recommend having a thread or pool of threads to which you give work as needed.  You'll need some synchronization mechanism so that the threads wait for work and then signal your main thread when they're finished.

Share this post


Link to post
Share on other sites

I would create the thread once and make a semaphore which the thread waits on. Then I would signal this semaphore each time I wanted the thread to wake up and process new work for it. Once the thread has completed the work it would then wait (sleep) for the next signal. 

Share this post


Link to post
Share on other sites

One thread per core is optimal, go beyond that and you start wasting CPU by switching between threads that have to wait for each other to run.

Share this post


Link to post
Share on other sites

Thank you all for your advices, I'll change my design then. I'll look at a single thread with semaphore, I think this is the way to go. Again, thank you all to help me make a decision.

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!