1

I believe I have a reasonable understanding of threading from an Object Oriented perspective using the Thread class and the Runnable interface. In one of my applications there is a "download" button that allows the user to run a task in the background that takes about half an hour whilst continuing to use the VB.NET application.

However, I do not understand how Threading maps to the physical architecture of a computer. If you have a single threaded application that runs on a PC with a quadcore processor then does a .net program use all four processors?

If you have a multi threaded application (say four threads) on a quadcore processor then does each thread execute on different cores?

Do you have any control of this as a developer?

I have referenced a book I read at university called Operating System Concepts, but I have not found a specific answer.

w0051977
  • 15,099
  • 32
  • 152
  • 329
  • A "download button" would suggest that your program is I/O bound, not compute bound, only running as fast as some server can supply the data. Easy to tell from Task Manager, if your program isn't burning 100% cycles on one core then adding threads isn't going to make it faster. – Hans Passant Feb 17 '13 at 18:36

2 Answers2

1

If you have a single threaded application that runs on a PC with a quadcore processor then does a .net program use all four processors?

No, it can’t, at least not simultaneously. However, it’s theoretically possible that the operating system’s scheduler first executes your thread on one processor and later moves it to another processor. Such a scheduler is necessary to allow simultaneously running more applications / threads than there are physical processors present: execution of a thread is sliced into small pieces, which are fed to the processor(s) one after the other. Each threads gets some time slice allocated during which it can calculate before usage of the CPU switches to another thread.

Do you have any control of this as a developer?

Not directly. What you can control is the priority of your thread to make it more important to the task scheduler.

On a more general note, you should not use threads in your use-case – at least not directly. Threads are actually pretty low-level primitives. For your specific use-case, there’s a component called BackgroundWorker which abstracts many of the low-level details of thread management for you.

If you have a multi threaded application (say four threads) on a quadcore processor then does each thread execute on different cores?

Not necessarily; again, the application has next to no control over how exactly its threads are executed; the operating system however tries really hard to schedule threads “smartly”. This means that in practice, if your application has several busy threads, they are spread out as evenly as possible across the available cores. In particular, if there are no more threads than cores then each thread gets its own core.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • Thanks, +1 for the last paragraph. This is what I have done. I often get threading and asynchronous programming mixed up. Can you explain the difference between threading and asynchronous processing? – w0051977 Feb 16 '13 at 18:49
  • Can you answer the question in the third paragraph of the original post? I should then be able to mark this question as answered. – w0051977 Feb 17 '13 at 16:20
  • @w0051977 Oh, I completely missed that part. Does my edit answer this? – Konrad Rudolph Feb 17 '13 at 16:37
0

Generally, you do not need to worry about mapping to physical architecture, .NET and the OS will do their best to maximize efficiency of your application. Just make it multi-threaded, even at the cost of being slower on a single threaded computer. You can, however, limit your maximum number of threads (if your app theoretically scales to infinity), to a number of cores, or double that. Test performance for each case, and decide which maximum works best for you.

Sometimes setting a core # can make your app's performance even worse. For example, if core #1 is currently scanning your PC for viruses, and your antivirus is single threaded. With the above scenario, assuming a quad-core PC, you do NOT want to run your 4-threaded app on a 1-per-core basis.

Having said that, if you really want to run a thread on specific core, it is possible - see this:

Also check this out:

Community
  • 1
  • 1
Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151