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?
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.
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.
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.
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.
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.
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:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With