Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default threads in c# very small console application ( Visual Studio 2012 ) [duplicate]

I am trying to figure out what is the purpose of the threads that appear in every new c# application. I created a new console application with empty Main function:

static void Main(string[] args)
{
}

and put a break-point on the end of the function, then I looked on the threads window:

Default Threads

Sometimes it shows 8 threads and sometimes 7 threads.

Can anyone explain what is the purpose of all those threads, and why do I need them for such a simple project?

like image 648
Alon Avatar asked Oct 18 '13 13:10

Alon


People also ask

What are threads in C?

A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is called a thread, and each thread defines a separate path of execution. C does not contain any built-in support for multithreaded applications.

What is the maximum number of threads in C?

The maximum number of threads per process is 512. The maximum number of threads can be retrieved at compilation time using the PTHREAD_THREADS_MAX symbolic constant defined in the pthread.

Is C single threaded?

I was recently reading "The C Programming language" by Ritchie, I noticed that C is a single threaded language.

Is Main a thread in C?

No, you have misunderstood. Every C program has a function named main() . C language semantics of the program start with the initial entry into that function. In that sense, and especially when you supply the parentheses, main() , is a function, not a thread.


2 Answers

In brief, these extra threads are GC, Finalizer, VS, and Debugger related. The link below provides a more detailed answer to your question:

Why does this simple .NET console app have so many threads?

like image 87
Sadek Noureddine Avatar answered Oct 13 '22 18:10

Sadek Noureddine


First of all I think we need to understand what are threads?

Threads:

Threading enables your program to perform concurrent processing so that you can do more than one operation at a time. For example, you can load a heavy images to your application, perform background tasks, and at that time you can handle a streaming to files.

If you would not use threads - then when you were loading the images to your application then your UI were stucked so you couldn't do nothing else, just waiting until the images will finish loading.

So why our application starts at 7-8 threads?

So lets see which threads we have:

By default, a C# program has one thread. This thread executes the code in the program starting and ending with the Main method.

You have also a garbage collector thread which responsible for killing objects when their life cycle ends.

And there are some more threads of debugging.

like image 32
Misha Zaslavsky Avatar answered Oct 13 '22 19:10

Misha Zaslavsky