How to break through the maximum number of threads for a single process in Windows by 2000

Source: Internet
Author: User

 

This problem occurs because, in Windows 32-bit systems, a process can use 2 GB of virtual memory, while a thread's default stacksize is 1024 K (1 m ), in this way, when the number of threads approaches 2000, 2000*1024 K = 2G (approximately), memory resources are equivalent to depletion.

Method 1: windows2003sdk is required
Does windows have a limit of 2000 threads per process?
Often I see people asking why they can't create more than around 2000 threads in a process. the reason is not that there is any participant limit inherent in windows. rather, the programmer failed to take into account the amount of address space each thread uses.

A thread consists of some memory in kernel mode (kernel stacks and Object Management), some memory in user mode (the thread environment block, thread-local storage, that sort of thing ), plus its stack. (or stacks if you're on an itanium system .)

Usually, the limiting factor is the stack size.

 

# include
# include
DWORD callback threadproc (void *)
{< br> sleep (infinite);
return 0;
}< br> int _ cdecl main (INT argc, const char * argv [])
{< br> int I;
for (I = 0; I <100000; I ++)
{< br> DWORD ID;
handle H = createthread (null, 0, threadproc, null, 0, & ID);
If (! H)
break;
closehandle (h);
}< br> printf ("created % d threads \ n", I); return 0;
}

This program will typically print a value around 2000 for the number of threads.

Why does it give up at around 2000?

Because the default stack size assigned by the linker is 1 MB, and 2000 stacks times 1 MB per stack equals around 2 GB, which is how much address space is available to user-mode programs.

You can try to squeeze more threads into your process by grouping your stack size, which can be done either by tweaking Linker options or Manually overriding the stack size passed to the createthread functions as described in msdn.

Handle H = createthread (null, 4096, threadproc, null, stack_size_param_is_a_reservation, & ID );

With this change, I was able to squeak in around 13000 threads. while that's certainly better than 2000, it's short of the naive expectation of 500,000 threads. (A thread is using 4kb of stack in 2 GB address space .) but you're forgetting the other overhead. address space allocation granularity is 64kb, so each thread's stack occupies 64kb of address space even if only 4kb of it is used. plus of course you don't have free reign over all 2 GB of the address space; there are system DLLs and other things occupying it.

But the real question that is raised whenever somebody asks, "What's the maximum number of threads that a process can create? "Is" why are you creating so far threads that this even becomes an issue? "

The "one thread per client" model is well-known not to scale beyond a dozen clients or so. if you're going to be handling more than that has clients simultaneously, you should move to a model where instead of dedicating 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-item-based model.

Note that fibers do not help much here, because a fiber has a stack, and it is the address space required by the stack that is the limiting factor nearly all of the time.

Method 2: Add connection Parameters

/Stack: reverse [, commit]

Note the difference between committed size and reserved size.
By default (unless dwcreationflags is set to stack_size_param_is_a_reservation), dwstacksize is used to adjust the space from the beginning to the stack, that is, initially committed size.

After the committed size is adjusted, how can the reserved size be adjusted?
If dwstacksize is smaller than the default reserve size, the reserve size uses the default reserve size;
If dwstacksize is greater than the default reserve size, the reserve size is rounded up to an integer multiple of 1 MB.

If the default stacksize is 64 K,/Stack: 65536 is set.

Method 3: Use the tool editbin.exe (in the VC tool)

editbin/stacksize: reverse [, commit]

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.