3

I have a single threaded application that runs on a machine with a quad core processor. The scheduled tasks that run VB.NET forms are too slow.

I am new to multi threading and parallel computing. If you have a single threaded application that runs on a server with a multi core processor then does the application only ever use one of the processors? What happens if you have multiple scheduled tasks and multiple instances are in memory at the same time?

I have read this question on Stackoverflow: https://stackoverflow.com/questions/607775/how-to-write-net-applications-that-utilize-multi-core-processors, but I am still not clear.

3
  • @JB King, thanks. There is one singkle wuad core processor. Could you provide this in an answer?
    – w0051977
    Sep 19, 2012 at 18:02
  • Judging by your other question I don't think concurrency is the issue you're struggling with so much as resource usage. The term "resource" can mean many things, open files are resources, open network connections like to a database, a shared memory space in ram etc. When you have multiple threads or multiple processes accessing the same resource, you do have to be careful that it can be safely accessed. i.e. if two threads or processes simultaneously try writing "Hello" to a file, it may get written "HHeelloo" (unlikely to work that way, being illustrative) when you want "HelloHello". Oct 19, 2012 at 21:06
  • As @Telastyn said however, if you have two threads/processes simply performing a calculation and not using a shared resource then they aren't fighting over a resource. Also note, a method is not a "resource" and is therefore accessible to many threads/processes simultaneously safely. Question is does that method access shared resources? Oct 19, 2012 at 21:08

4 Answers 4

3

A process will have one thread, unless you tell it otherwise. Two processes then have two threads total. Each core on a processor can run one thread at a time. So, if you have a processor with 2 cores (or 2 processors with 1 core each):

  • If you have one process with one thread, it will only ever run on one of the cores at a time (note that it can switch between cores, but will never use both at the same time).

  • If you have two processes with one thread each, they can (but might not) both run at the same time (on different cores and/or different processors).

  • If you have one process with two threads, both threads can (but might not) run at the same time (on different cores and/or different processors).

3
  • Please see a question I asked a while ago here: stackoverflow.com/questions/8243020/…. Your answer to this question suggests that you think that function 3 could be called simultaneously?
    – w0051977
    Sep 19, 2012 at 18:12
  • @w0051977 - it depends on what it does. If it just does calculations, it should be fine. If it tries to read/write from the same files, it probably won't.
    – Telastyn
    Sep 19, 2012 at 18:26
  • +1 for detailing the bounds, understanding from these 3 points you can basically deduce the whole behavior of threads/processes/cores Oct 19, 2012 at 20:57
1

"If you have a single threaded application that runs on a server with a multi core processor then does the application only ever use one of the processors?" You don't state how many processors the server has so it could be that there is just one processor but with multiple cores and thus only one processors is getting used as there aren't other processors to use. The warning here is to beware of the difference between the number of cores and the number of processors as there is something to be said for which perspective are you asking the question. For example, a single quad-core processor isn't quite the same as 2 dual-core processors and thus I'd advise caution in your word choice as core and processor aren't interchangeable terms for me.

If there are multiple instances in different processes then they should run in different address spaces in memory would be my guess but that is more up to the OS than anything else.

Something to consider here is what kind of forms are you using here: Winforms or Webforms? If the latter, even though each page may use a single thread, the application pool used by IIS may have multiple threads and thus the code is executed on multiple cores. If it is a Winforms application, I don't know the answer.


Introducing Threading may or may not encourage the OS to use other processors, as there is the concept of Concurrent computing which can complicate the question as there can be the appearance of parallel execution. Thus one could bring in multiple threads but this still gets executed on the same processor possibly.

2
  • is it fair to say that introducing Threading would not "encourage" the OS to use multiple processors?
    – w0051977
    Sep 19, 2012 at 18:04
  • @w0051977: What JB King was trying to say with that is that, if you spin up more than 1 thread, there is a possibility, but not a guarantee, that those threads will run on separate cores/CPUs. That would be up to the OS/Runtime thread scheduler. Oct 19, 2012 at 20:46
1

A single-threaded application can only run on a single core, it well never fully utilize a processor. If you perform concurrent work within that application, for each thread you create, it can be assigned to a different core. Intels current generation of multi-core processors tend to have hyper threading on them as well. So for example a quad core with hyper-threading has 8 virtual cores, (if you view the task manager performance tab, you will see the CPU usage divided into 8 columns). This means that windows will run 8 separate threads at the same time.

The simplest path to making your application multi-thread is to use the Task Parallel Library which is available for .NET 4 and above.

1
  • Can you have a look at a question I asked here: stackoverflow.com/questions/8243020/…. Function 3 reads and writes to a database. Are you saying that function 1 and function 2 could access function 3 concurrently?
    – w0051977
    Sep 19, 2012 at 18:30
1

It will depend on server hardware, OS and environment where your code runs.

In another words, it's up to the OS where a thread is scheduled. There are advantages to keeping a thread on the same core if possible, in terms of cache coherency etc. And forcing it to stay on the same core is usually over restrictive.

There are core differences in approach for single threaded and multi-threaded application development that have to be taken into account. For .NET platform developers, here is a really good article that unfolds essentials to be aware of - Multi-threading in .NET: Introduction and suggestions

That post has very important points to enlighten understanding of .NET threading processes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.