Does Windows have a limit of 2000 threads per process?
http://blogs.msdn.com/b/oldnewthing/archive/2005/07/29/444912.aspx
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 particular 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 <stdio.h>
#include <windows.h> DWORD CALLBACK ThreadProc(void*)
{
Sleep(INFINITE);
return 0;
} int __cdecl main(int argc, const char* argv[])
{
int i;
for (i = 0; i < 100000; i++) {
DWORD id;
HANDLE h = CreateThread(NULL, 0, ThreadProc, NULL, 0, &id);
if (!h) break;
CloseHandle(h);
}
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 1MB, and 2000 stacks times 1MB per stack equals around 2GB, which is how much address space is available to user-mode programs.
You can try to squeeze more threads into your process by reducing 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 2GB 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 2GB 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 many 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 many 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.
Does Windows have a limit of 2000 threads per process?的更多相关文章
- ORA-20000:ORU-10027:buffer overflow,limit of 2000 bytes.
ORA-20000:ORU-10027:buffer overflow,limit of 2000 bytes. 这是因为在过程中用到了dbms_output.put_line()在服务器端输出信 ...
- ORA-20000 ORU-10027 buffer overflow limit of 2000 bytes
这是在pl/sql中执行存储过程报的错,原因是serveroutput限制存储, 解决方案:set serveroutput on size 10000000;
- Windows 7 X64 SQL Server 2000 企业管理器无法建立新表
问题现象: 当建立新表时会出现一个空白的窗口,敲击键盘输入后就会出现应用出错,然后退出. 解决方案: 选中某个已经存在的表,再点右键,新建表就可以正常操作了.
- node.js在windows下的学习笔记(8)---进程管理Process
process是一个全局内置对象,可以在代码中的任何位置访问此对象,这个对象代表我们的node.js代码宿主的操作系统进程对象. 使用process对象可以截获进程的异常.退出等事件,也可以获取进程的 ...
- C10K问题渣翻译
The C10K problem [Help save the best Linux news source on the web -- subscribe to Linux Weekly News! ...
- (转)The C10K problem翻译
The C10K problem 如今的web服务器需要同时处理一万个以上的客户端了,难道不是吗?毕竟如今的网络是个big place了. 现在的计算机也很强大了,你只需要花大概$1200就可以买一个 ...
- 关于Socket 多线程 的一篇好文章
http://www.kegel.com/c10k.html#topIt's time for web servers to handle ten thousand clients simultane ...
- [Oracle] UNIX与Windows 2000上Oracle的差异(III)
作者:Ian Adam & David Stien, SAIC Ltd 日期:19-Dec-2003 出处:http://www.dbanotes.net翻译:Fenng ORACLE 的安装 ...
- WINDOWS操作系统中可以允许最大的线程数
默认情况下,一个线程的栈要预留1M的内存空间 而一个进程中可用的内存空间只有2G,所以理论上一个进程中最多可以开2048个线程 但是内存当然不可能完全拿来作线程的栈,所以实际数目要比这个值要小. ...
随机推荐
- BZOJ1484 [HNOI2009]通往城堡之路
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- PHP5+APACHE2.2配置
注意这里用的是PHP5.3版本 在Windows系统上使用Apache2.2上模块化安装PHP5.3 PHP5.3增加了一些功能,如namespace,静态迟绑定等. 本篇文章将帮您如何安装PHP5. ...
- VFIO简介
VFIO是一套用户态驱动框架,它提供两种基本服务: 向用户态提供访问硬件设备的接口 向用户态提供配置IOMMU的接口 VFIO由平台无关的接口层与平台相关的实现层组成.接口层将服务抽象为IOCTL命令 ...
- java-四则运算-四
题目要求:查找数组连成环形的和最大的连续子数组 实验代码: package zuoYe; import java.util.Scanner; public class MaxSubArray { pu ...
- js 格式化时间日期函数小结3
function DateUtil(){}/***功能:格式化时间*示例:DateUtil.Format("yyyy/MM/dd","Thu Nov 9 20:30:37 ...
- int(3)与int(11)的区别
注意:这里的M代表的并不是存储在数据库中的具体的长度,以前总是会误以为int(3)只能存储3个长度的数字,int(11)就会存储11个长度的数字,这是大错特错的.其实当我们在选择使用int的类型的时候 ...
- (转)程序员应该知道的10个eclipse调试技巧
调试不仅可以查找到应用程序缺陷所在,还可以解决缺陷.对于Java程序员来说,他们不仅要学会如何在Eclipse里面开发像样的程序,更需要学会如何调试程序.本文介绍了Java程序员必知的10个调试技巧, ...
- 如何在eclipse中安装ADT
打开Eclipse,点击 Help -> Install New Software: 点击Add: 然后点击Archive,添加对应的上面的ADT-21.0.1.zip: OK后,再写上一个名字 ...
- tf随笔-1
生成新的计算图,并完成常量初始化,在新的计算 图中完成加法计算 import tensorflow as tf g1=tf.Graph() with g1.as_default(): value=[1 ...
- 设计模式之桥接(bridge)模式
在现实生活中,我们常常会用到两种或多种类型的笔,比如毛笔和蜡笔.假设我们需要大.中.小三种类型的画笔来绘制12中不同的颜色,如果我们使用蜡笔,需要准备3*12=36支.但如果使用毛笔的话,只需要提供3 ...