Vicky Liao
Vicky Liao

Reputation: 17

multithread running on difference processes or same process?

in my .net multithread program, i am wondering all these threads running on the same process or different processes? if it is on the same process, then i assume one process run on one core, then how multithreading can utilize all the four cores that i have in my quad-core cpu? but if it is on the different processes, as i know different processes and same process have different data sharing mechanism, then how come i don't need to write different code to handle this in my multithreading program? Would anyone shed some light on

I want to ask two more similar questions

When i open the task manager, often times, i can see around 800 threads and 54 processes,and my cpu usage is only 5%,and i was told that each core only excute one thread at a time. is my cpu running these 800 threads all the times, or only means 800 threads are queuing, waiting cpu to process? if i want my multithreading program fully utilze my quad-core cpu, can i raise the cpu usage by creating more threads(it seems contradict the theroy that only one thread one core at a time)

Upvotes: 2

Views: 1404

Answers (4)

Kiril
Kiril

Reputation: 40395

in my .net multithread program, i am wondering all these threads running on the same process or different processes?

A thread always runs in a process, however, multiple threads can run in a single process and each thread can be handled by a different core.

Threading

If you have a single core, it doesn't mean that it can't run multiple threads, it just means that the core can't execute multiple threads at the same time. If you take a look at the picture above, you will note that:

  1. Thread #1 executes for some time.
  2. Thread #1 "stops".
  3. Thread #2 executes for some time.
  4. Thread #2 "stops".
  5. Thread #1 executes for some time, again.

This illustrates what happens when a core runs multiple threads: the core only executes one thread at a time, but in order for both threads to run, the core must perform context switching. In other words: the core runs a few commands from Thread 1, switches to Thread 2 and runs a few commands from it, then it switches back to Thread 1 to execute some more commands.

Juggling Oranges:

A good metaphor is juggling oranges: technically, you only have two hands and you can only hold one orange in each hand at a time, so the maximum you can hold is two oranges. In this case the taxing part is holding the oranges. However, if you throw an orange up in the air, then you can hold a 3rd orange while the the 2nd one is in the air. The higher you throw the oranges, the more oranges you can juggle. To be more precise: the longer it takes for an orange to come back in your hand, the more oranges you can juggle. Of course, you probably can't juggle an enormous amount of oranges, because throwing an orange requires more energy than simply holding it.

In essence, your CPU is juggling threads: the longer a thread stays away from executing code on the CPU, the more threads a CPU can "juggle." If a thread is waiting on I/O (e.g. a database request), then the CPU can execute the code of another thread at the same time. This is the same reason why you see 54 processes and 800 threads in the task manager: many of those threads are doing things that are not CPU-bound.

Sleep:

is my cpu running these 800 threads all the times, or only means 800 threads are queuing, waiting cpu to process?

Many of the threads you're noticing in your task manager are idle/sleeping, so they use very little (if any) CPU. However, the ones that are running are executed with context switching (if there are more threads than cores, which is the case most of the time). There are many things that can cause a thread to idle/sleep, see the orange juggling for an example.

CPU Utilization:

if i want my multithreading program fully utilze my quad-core cpu, can i raise the cpu usage by creating more threads(it seems contradict the theroy that only one thread one core at a time)

It gets tricky :). Imagine that instead of oranges, you have bowling balls: it's VERY taxing on your hands, so even if you tried, you probably won't be able to hold more than 2 bowling balls let alone juggle a 3rd one. At maximum load, you can only hold as many objects as you have hands. The same is true for the CPU: at maximum load, the CPU can only execute as many threads as there are cores.

The reason why you can run more threads than the number of cores is because the thread are not putting the maximum load on the cores. If your threads are CPU bound, i.e. they do some heavy computational stuff and they tax the core 100%, then you can only run as many threads as you have cores. However, the CPU is the fastest thing in your computer and your thread may be accessing other parts of your computer that are significantly slower than your CPU (hard disk, network card, etc), so you can run more threads.

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1064244

A single process may use a number of threads; even a basic .NET "hello world" console exe probably uses 4 or 5. So yes, a single process can potentially use all your available cores if you write it to do so.

Because it is the same process, data sharing is direct, but: care must be taken if you are changing the values, as otherwise very bad things can happen. Access must be carefully synchronized (lock etc) if you are changing the data within the threaded code.

You do, however, usually have to write different code to support multiple threads. Exceptions to this is when the framework is doing that for you, for example, ASP.NET or WCF may take incoming requests and hand them to different worker threads, allowing multiple concurrent operations even though you didn't explicitly code it that way. Which means that in ASP.NET or WCF you need to be careful with shared state, for exactly the reasons already discussed.

As a minor addition, note also that a process can support multiple AppDomains; in that scenario, the threads for the process are shared between all the AppDomains at whim by the scheduler.

Upvotes: 1

Yochai Timmer
Yochai Timmer

Reputation: 49269

Multithreading means multiple threads in the same process.
Each thread can be assigned to a different core.

But all the threads belong to the same process, for example if one of the threads will throw an unhandeled exception, the process will crash with all its threads.

You could have read a bit about it, just search google or Wikipedia - Software Multithreading

Upvotes: 1

Kylotan
Kylotan

Reputation: 18449

Threads created by that process are part of that process. Different threads within the one process can and often do run on different processors or processor cores.

Upvotes: 0

Related Questions