Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens when a .NET console app blocks on Console.ReadLine()?

I had an interesting interview question today. Consider you have the following console application:

static void Main(string[] args) 
{
    Console.WriteLine("Hello world");
    Console.ReadLine();
}

When the Console.ReadLine() line is reached execution is suspended and the program waits for input from the keyboard. How many threads are there at this point and what state are they in, e.g. Running, Suspended, etc. ?

I suppose what the interviewer was after was awareness/understanding of the threads that make up a .NET console app and how they work together to interact with the IO subsystem of the underlying OS.

like image 523
alex.tashev Avatar asked Jun 15 '16 11:06

alex.tashev


2 Answers

There's no definite number.

Your code is only running on one thread - the main thread. Calling Console.ReadLine doesn't create a new thread, it just starts an I/O request and waits for it to be processed - this requires no threads, but since you're using the synchronous API, there's no way to release your thread, so it just blocks. If the interviewer wanted just one number, this is the number - it's the only application thread you have, everything else is an implementation detail.

There's a lot of infrastructure threads - the Garbage Collector threads are the main ones, and some of those are created and destroyed all the time.

Finally, there's the threads somewhere in between - most notably, the thread pool. Since you're not using the thread-pool, it will have the default number of threads - unless the configuration says otherwise, this is usually two threads per logical CPU core.

The reality goes even deeper. For example, .NET threads are managed threads, and they may skip between "native" threads at the will of the runtime. However, this is something you never really need to care about unless you're writing your own .NET runtime or an operating system :) Or, doing heavy interop with native code that depends on native thread-affinity (again, not very common).

I suspect the main purpose of the question is to get you talking about how the Windows threading model works, how .NET handles threads and what kinds of threads there are in a typical windows/.NET application. But mainly, to get you to talk :P

like image 181
Luaan Avatar answered Oct 20 '22 01:10

Luaan


I agree with Luuan, therefore, depending on what your interviewer meant, there is for example VisualStudio threads running in addition to the Main thread. BTW, this thread can be disabled (In VS2015: Project Properties > Debug tab > uncheck Enable the Visual Studio hosting Process)

If you are using Visual Studio, you should be able to see the threads during debugging.

The running code:

static void Main(string[] args)
{
    Console.WriteLine("Hello world");           
    Console.ReadLine();
}

The Thread Window (In VS2015: Debug -> Windows -> Threads):

enter image description here

I added a new thread to your example:

The running code:

static void Main(string[] args)
{
    Console.WriteLine("Hello world");
    new Thread(DoSomethingElse).Start();            
    Console.ReadLine();
}

private static void DoSomethingElse()
{
    while (true)
    {
        Thread.SpinWait(100);
    }
}

The Thread Window:

enter image description here

like image 41
ehh Avatar answered Oct 20 '22 01:10

ehh