Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens if you start too many threads?

What happens when you create too many threads? Does it cause the CPU to crash or is there some sort of internal load balancing mechanism on Windows OS'?

I'm running the following code:

private async void A(string[] a)
{
    var tasks = a.Select(B);
    await Task.WhenAll(tasks);
}

private async Task B(string b)
{
    new Thread(async delegate ()
    {
        //all the work that needs to be done

    }).Start();
}

I'm running an array of async Tasks but within each async method I've encapsulated all the work that needs to be done in a new thread. What happens if I call B a large number of times? How will the processor deal with too many threads?

like image 881
Skylar Kennedy Avatar asked Oct 21 '25 10:10

Skylar Kennedy


1 Answers

The CPU only executes what the OS tells it to, the OS is in charge of which threads run and how long they run before they are interrupted. There is some anti-starvation built into the scheduler so it should never completely lock the system but you can probably almost bring it to its knees if you just keep spawning as many threads as possible until you run out of memory or address space.

If we pretend that your program is the only program running then the ideal number of threads is the same as the number of CPU cores if the task is CPU limited. If the task is I/O limited or needs to wait on kernel objects then more threads might be ideal.

If you create thousands of threads then you will waste time context switching between them and your work will take longer to complete. Instead of manually starting new threads you should use the thread pool to perform your work so Windows itself can balance the optimum number of threads.

await and other high level asynchronous keywords probably already use the thread pool.

like image 67
Anders Avatar answered Oct 23 '25 01:10

Anders



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!