Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we create 300,000 threads in a C# application and run it on a PC?

I am trying to imitate a scenario where 300,000 consumers are accessing a server. So I am trying to create the pseudo clients, by repeatedly querying the server from the concurrent threads.

But the first hurdle to be cleared is, whether it is possible to run 300,000 threads on a PC? Here is a code which I am using to see intially how many max threads I can get, and later then replace the test function with the actual function:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace CheckThread
{
    class Program
    {
        static int count;

        public static void TestThread(int i)
        {
            while (true)
            {
                Console.Write("\rThread Executing : {0}", i);
                Thread.Sleep(500);
            }
        }

        static void Main(string[] args)
        {
            count = 0;
            int limit = 0;
            if (args.Length != 1)
            {
                Console.WriteLine("Usage CheckThread <number of threads>");
                return;
            }
            else
            {
                limit = Convert.ToInt32(args[0]);
            }
            Console.WriteLine();
            while (count < limit)
            {
                ThreadStart newThread = new ThreadStart(delegate { TestThread(count); });
                Thread mythread = new Thread(newThread);
                mythread.Start();
                Console.WriteLine("Thread # {0}", count++);
            }

            while (true)
            {
                Thread.Sleep(30*1000);
            }
        } // end of main
    } // end of CheckThread class
} // end of namespace

Now what I am trying might be unrealistic, but still, if there is a way out to do it and you know, then please help me.

like image 644
Andy_MSFT Avatar asked Jun 07 '11 14:06

Andy_MSFT


People also ask

How many threads can you create?

On Windows machines, there's no limit specified for threads. Thus, we can create as many threads as we want, until our system runs out of available system memory.

How many threads can run at once?

A single CPU core can have up-to 2 threads per core. For example, if a CPU is dual core (i.e., 2 cores) it will have 4 threads.

How many threads can C# Run?

The maximum allowed number of processing threads in a pool is 1023. The pool allocates a maximum of 1000 threads in an I/O operation. To get maximum number of threads, you can use the GetMaxThreads method of the ThreadPool static class.

What happens if you have too many threads?

The Case of Creating Too Many Threads. Our job will take longer to finish if we generate thousands of threads since we'll have to spend time switching between their contexts. Use the thread pool to complete our task rather than creating new threads manually so that the OS can balance the ideal number of threads.


4 Answers

Each thread will create its own stack and local storage, you are looking at roughly 512k of stack space per thread on a 32bit OS, I think the stack space doubles on a 64 bit OS. A quick back of the spreadsheet calc gives us 146.484375 gigs of stack space for your 300k clients.

So, no, don't create 300k threads, but rather use the threadpool to simulate 300k requests, although tbh I think you would be better off with several test clients spamming your server through a network interface.

There are a lot of web load-testing tools available. Good starting point : http://www.webperformance.com/library/reports/TestingAspDotNet/

like image 155
Florian Doyon Avatar answered Sep 19 '22 19:09

Florian Doyon


You can alter the maximum nunmber of threads by calling the ThreadPool.SetMaxThreads method. 300,000 threads will probably make your PC explode*

*This is probably an exaggeration

like image 28
Ben Robinson Avatar answered Sep 22 '22 19:09

Ben Robinson


Language-agnostic answer:

The better way to probably go about this is using the Reactor pattern, with a maximum of 1 or 2 concurrent threads per core.

like image 35
Evert Avatar answered Sep 22 '22 19:09

Evert


As .net commits the entire stack (1MB) for each clr thread; as Ben says, your PC may actually explode. Or possibly OoM.

like image 30
dashton Avatar answered Sep 21 '22 19:09

dashton