A Multithreading Question
Hi guys, a quick theory question.
When building a game I discovered the benefits of creating a separate thread to handle my path finding AI for all entities.
However once I had set this up I realised just how beneficial it was, so my question:
What downsides are there to using lots of threads, such as a separate update thread for every single entity in a game?
Re: A Multithreading Question
The threads can become out of sync. Google: Race Condition
Let's say we have a variable: var
Now let's say we have two threads: Thread_A, Thread_B
At a certain point in the program, Thread_A will try to read the value of var, while Thread_B will try to write the value of var.
Here's a question for you: which will happen first? Will Thread_A read the value of var before Thread_B writes to it, or will Thread_B write the value of var before Thread_A reads it?
The answer: there is no way to know. One could happen before the other some times, while the other could happen first other times.
To solve this issue, it is best to always synchronize threads before trying to access shared variables. Read: Synchronization (The Java™ Tutorials > Essential Classes > Concurrency)
Re: A Multithreading Question
Quote:
What downsides are there to using lots of threads, such as a separate update thread for every single entity in a game?
Lots of variables involved, but in general creating many threads at once can cause complications. Threads need to be created and managed (overhead), threads can deadlock, code must be synchronized, each thread requires free CPU, etc...these variables each cause resources to get bogged down (a UI could lock up, as can garbage collection or even the system itself). While you don't mention how many threads you are talking about, thread pools are often a better choice - dispatching the work (in a managed way) as necessary to the thread pool without overloading the system.
See Thread Pools (The Java™ Tutorials > Essential Classes > Concurrency)
Re: A Multithreading Question
Also, from a performance point of view, it is also important to remember that there really isn't such a thing as "true" multitasking. The number of processes which a computer can run at any given time depends on the number of CPUs it has access to. A CPU can only run a single process at a time. Computers *fake* multitasking by quickly cycling through processes, but ultimately only one of them is running at any given time.
Re: A Multithreading Question
Thanks guys that makes a lot of sense, one question though: lets say that theoretically we have no clashing variables and no additional complications, Would a program that uses 10 threads use more of the computers resources than a single threaded program of the same function?
Re: A Multithreading Question
Quote:
there really isn't such a thing as "true" multitasking
Not true for computers with more than one "core". Each core can be executing a thread at the same time as the other threads are being executed on other cores.
Many modern PCs have multiple cores.
I worked on a multiple core CPU in the mid 1980s.
Re: A Multithreading Question
Quote:
Originally Posted by
Norm
Not true for computers with more than one "core". Each core can be executing a thread at the same time as the other threads are being executed on other cores.
Many modern PCs have multiple cores.
I worked on a multiple core CPU in the mid 1980s.
lol, I ignored that. Just change all the instances of "CPU" in my previous post with "CPU Core", and I think we are ok.