Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why 3 threads for a basic single threaded c# console app?

I created a console app in c# with a single Console.ReadLine statement. Running this app within Visual Studio and stepping into the debugger shows 7 threads in the thread window (6 worker threads, one is .NET SystemEvents and the other is vshost.RunParkingWindow and the main thread).

When I run the app outside Visual Studio I see a total of 3 threads in Windows task manager. Why so many when I would expect 1 thread? What are the others being spawned for?

like image 218
NabilS Avatar asked Apr 17 '09 21:04

NabilS


People also ask

Why is it better to use multi threading polling instead of a single threading model?

Advantages of Multithreaded Processes All the threads of a process share its resources such as memory, data, files etc. A single application can have different threads within the same address space using resource sharing. It is more economical to use threads as they share the process resources.

Why we should use multi thread?

On a multiprocessor system, multiple threads can concurrently run on multiple CPUs. Therefore, multithreaded programs can run much faster than on a uniprocessor system. They can also be faster than a program using multiple processes, because threads require fewer resources and generate less overhead.

How multithreading improves performance over a single threaded solution?

The process of executing multiple threads simultaneously is known as multithreading. Some examples where multi threading improves performance include: Matrix multiplication — Individual rows and columns of the matrices can be multiplied in separate threads, reducing the wait time of the processor for addition.

Does C have multi threading?

C does not contain any built-in support for multithreaded applications. Instead, it relies entirely upon the operating system to provide this feature. This tutorial assumes that you are working on Linux OS and we are going to write multi-threaded C program using POSIX.


2 Answers

If you're running a .NET application, I believe you always get a thread (mostly sleeping) for the JIT (Just-in-Time compiler) as well as the GC (Garbage Collection) thread, in addition to your main thread.

like image 99
Reed Copsey Avatar answered Oct 14 '22 06:10

Reed Copsey


You do not need to worry: If you don't explicitly use them you won't have any of your code running in another thread than the main thread. The other threads are for:

  • Garbage collector
  • Finalization
  • Threadpool

Do the 3 threads share one stdin?

Theorethically yes, but the others won't use it unless you use Console.ReadLine inside a destructor or inside ThreadPool.QueueUserWorkItem, so don't worry you will get all data in main thread

like image 33
Zotta Avatar answered Oct 14 '22 05:10

Zotta