Results 1 to 3 of 3
Suppose I want to create threads for a task, what if it is done by a single thread? Is it true that multi-thread will do the same job in less ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 08-09-2012 #1
- Join Date
- Aug 2012
Efficient Thread creation
Suppose I want to create threads for a task, what if it is done by a single thread?
Is it true that multi-thread will do the same job in less time? I could never catch that argument, in what case it does, in what cases it doesn't?
What if I have 'm' cores, then what should be the optimized number of threads which will give the most efficient execution with respect of time.
What are the factors I need to think of, I need a mathematical and rational reasoning.
Does it depend on OS? for example how many should I for linux? for ubuntu? for windows xp?
If there is any conceptual technique to decide that, please help me with your knowledge or pointer to the same.
So if I decide that n number of threads will be best in terms of optimised time execution, so when should I check or decide to create another thread..
the problem is I can repeatedly check it for a constant time, or I can fire the creation only when one thread is dying. Help me it to visualize in C.
So If I prefer the second way, is there anyway in c or java when I can fire that. Any googleable term will also be helpful. Another problem is, I may not know how much the machine is loaded, will number of thread should depend on that?
I know my language is bad, so harse, but truth is for me knowledge is love. Thanks in advance.
- 08-10-2012 #2
- Join Date
- Aug 2012
Thread is mainly used for parrellel programming.(in reality they are concurrent not parallel ,i.e if you are having dual core only two process or thread can run parallel though if you create four threads then they will run concurrently ).and the time time allocation is entirely depndent on OS.
- 08-14-2012 #3
- Join Date
- Jun 2012
- SF Bay area
In general, the reason code will complete faster (in terms of wallclock time) when organized to run in multiple threads or in multiple processes, is that the OS can take advantage of idle resources if there's more than one process/threads it can schedule.
The most obvious case is, as you suggested, systems with mutiple CPU's or a single CPU with multiple cores. In that case, a program running on a system with X cores will run faster as it's split into more threads until X threads are run.
What's not as obvious, is that other factors also cause system resources to become idle. I/O requests are a probably the biggest causes of processes blocking, but other things like semaphores or code just blocking waiting for timers or other interrupts will also take a process/thread out of consideration by the scheduler.
So back to the X cores system, it's possible that the code run in X+Y threads will complete faster then when it's run with X threads. If the code does a lot of slow I/O, like pulling data from across the Internet over a crappy connection with poor bandwidth and packet loss, then the a cores on the system could be idle a significant amount of the time even with more then X threads running.
I think the answer to your general question of how many threads to run for maximum efficiency depends on what the code it doing, but except in strange situations it will be at least at many threads as your system has cores. And the more each thread can block for I/O, the more likely additional threads are to help.