PDA

View Full Version : question about multithreading



laggyluk
22-05-2013, 04:25 AM
So I'm running a pathfinder on separate thread. Seems that thread can't be 'restarted' so I'm wondering is it better to create it every time there is a path to find and self terminate afterwards or make him do nothing between the searches (and eat the core time for idling?). What do you guys think?

Andru
22-05-2013, 05:56 AM
If you use only one thread it's better to use event for waiting command "search new path" instead of creating the thread every time, because it takes additional time. Anyway you can extend this idea and implement some thread pool for searching more than one path.

laggyluk
22-05-2013, 07:34 AM
I plan to use another thread for running sound engine, and preferably no more than one thread per core (if its possible to have control over that, I don't know much about threads yet :P)

SilverWarior
22-05-2013, 08:49 AM
When doing multithreading don't be afraid to have more threads than you have actual numer of cores. Why? Some of theese treads may finish their job before others will. This means that the core on which one of such treads was running before is now idle.
Anywhay creating a few more threads that computer can simultanously use doesn't create so much slowdown that you should worry about that to much (take a look at multithreaded applications which are being runn on single core CPU). Sure overdooing it will definitly lead to noticable performance loss.
Best way is to try and test various scenarios. I would also recomend profiling code blocks which are being executed within your threads to get athleast some idea of how much time does each thread need for its work. This can help you in planning.

And yes using thread pool is a verry good idea becouse as it was sad before creating new thread do takes some time, in worst case scenario even upto 1ms which can be a lot. And yes thread creation time is largly afected by OS kernel usage. That's why high kernel usage (bad drivers, faulty hardware) can quickly lead to systemwide slowdowns or even ustability.

User137
22-05-2013, 11:06 AM
Thread is like a person. Even while "he" is working, you can give him a new assignment on paper, and tell to stop whatever it is he's doing :) If you have reference to that thread stored in main thread, you can write into its variables, such as newTask: TTaskInfo; whatever it contains. In pathfinding, new task needs to know points A and B at least, and the fact that current job must be started over with new info. But then you need to realize, that you must make copy of the task information once "working" starts. So that current and new task don't get mixed.

laggyluk
22-05-2013, 11:14 PM
currently there's a list of paths to find and when there's something on the list and thread is nil then first item is copied to a var used by thread to get data and then it's created. I'll make him persistent and waiting for new stuff then.
thanks for the replies