Maximum number of threads allowed in the Windows operating system (thread stack reserved for 1M space) (56 Windows blogs worth a look)

Source: Internet
Author: User

Maximum number of threads that can be allowed in the Windows operating system

By default, a thread's stack is reserved for 1M of memory space
While the memory space available in a process is only 2 G, it is theoretically possible to open up to 2048 threads in a process
But of course the memory is not completely taken as a thread stack, so the actual number is smaller than this value.
You can also change the default stack size by connecting, and make it smaller so that you can open more threads.
If you change the size of the default stack to 512K, you can theoretically open up to 4,096 threads.

Even if the physical memory is large, the thread that can be played in a process is always limited by the memory space of 2GB.
Let's say your machine has 64GB of physical memory, but each process has a memory space of 4GB, where the user state is still available in 2GB.


If it is within the same machine, the number of threads that can be set up is also limited by memory. Each thread object has to stand on non-paged memory, not page memory, and when non-page memory is exhausted, the thread cannot be created.

If the physical memory is very large, the limit on the number of threads that can be run within the same machine is increasing.

Write a program under Windows, a process fork out 2000 left and right threads will be abnormally exited, why?

This problem arises because of the WINDOWS32 bit system, where the maximum virtual memory that a process can use is 2G, and the default line stacks stacksize for a thread is 1024K (1M), so that when the number of threads approaches 2000, 2000*1024k= 2G (approximately), memory resources are equivalent to exhaustion.

MSDN Original:

"The number of threads a process can create is limited by the available virtual memory. By default, the every thread has one megabyte of stack space. Therefore, you can-create at the most 2,028 threads. If you reduce the default stack size, you can create more threads. However, your application would have better performance if you create one thread per processor and build queues of requests For which the application maintains the context information. A thread would process all requests in a queue before processing requests in the next queue. "

How do I break 2000 limits?

You can reduce the line stacks stacksize by modifying the CreateThread parameter, for example

#define   Max_threads   50000
DWORD   WINAPI   threadproc (   lpvoid   lpparam   ) {
while (1) {
Sleep (100000);
}
return   0;
}
int   Main ()   {
DWORD   Dwthreadid[max_threads];
HANDLE   Hthread[max_threads];
for (int   i   =   0;   I   <   max_threads;   ++i)
{
Hthread[i]  = CreateThread (0, +  , ThreadProc, 0, stack_size_param_is_a_reservation,   &dwthreadid[ I]);
if (0   = =   Hthread[i])
{
DWORD   E   =   GetLastError ();
printf ("%d\r\n", e);
Break
}
}
ThreadProc (0);
}

Server-side programming

If your server is programmed to create a thread for a client connection request, there are 2000 restrictions (in the case of hardware memory and CPU count). The recommendations are as follows:

The "one thread per client" model is well-known does not be beyond a dozen clients or so. If you ' re going to being handling more than that many clients simultaneously, you should move to a model where instead of ded Icating a thread to a client, you instead allocate an object. (Someday I ' ll muse on the duality between threads and objects.) Windows provides I/O completion ports and a thread pool to help you convert from a thread-based model to a work-i Tem-based model.

1. Serve many clients with each thread, and use nonblocking I/O and level-triggeredreadiness Notification2. Serve many clients with each thread, and use nonblocking I/O and readiness Change notification3. Serve Many clients with all server thread, and use asynchronous I/O

Http://www.cnblogs.com/lancidie/archive/2011/12/15/2289337.html

Maximum number of threads allowed in the Windows operating system (thread stack reserved for 1M space) (56 Windows blogs worth a look)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.