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个线程 但是内存当然不可能完全拿来作线程的栈,所以实际数目要比这个值要小. ...
随机推荐
- Windows安装Ubuntu桌面操作系统到移动硬盘中以及错误解决
用到的工具:U盘一个(usb3.0,你懂的),移动硬盘(我这个是笔记本里面取出来的机械硬盘装上的盒子) 第一步:下载Ubuntu系统iso镜像文件 下载Ubuntu系统iso镜像文件,由于我是新手,下 ...
- 指定library路径
1.执行 ?.jar文件: 1.1.“java -jar ?.jar” 1.2.如果 ?.jar里面使用了JNI调用了 ?.dll/?.so 等文件,可能会报错 找不到相关的 库文件,如果这样的话,可 ...
- 报错HTTP Status 500 - Unable to instantiate Action
报错如下: HTTP Status 500 - Unable to instantiate Action, visitAction, defined for 'visit_toAddPage' in ...
- yii2:不使用composer安装yii2-jui的方法
今天有一个功能需要用到autocomplete,既然用yii2开发,在这里当然使用它自带的yii2-jui中的autocomplete组件了.yii2basic版默认是没有yii2-jui组件的,需要 ...
- shell基础复习笔记
变量的设置 以等号连接,等号两边不能有空格 变量名首个字符必须是英文,可以使用下划线,不能使用标点符号,不能使用bash里的关键字 可以使用转义字符\将特殊符号(如Enter.$.空格.!等)变成一般 ...
- git一个系列教程
https://git-scm.com/book/zh/v1/%E8%B5%B7%E6%AD%A5-%E5%85%B3%E4%BA%8E%E7%89%88%E6%9C%AC%E6%8E%A7%E5%8 ...
- laravel中Blade模板继承
Blade模板继承 和 区块 <!-- 文件保存于 resources/views/layouts/app.blade.php --> <html> <head> ...
- Ajax中的XMLHttpRequest对象详解(转)
XMLHttpRequest对象是Ajax技术的核心.在Internet Explorer 5中,XMLHttpRequest对象以ActiveX对象引入,被称之为XMLHTTP,它是一种支持异步请求 ...
- inline,block,inline-block解析
display:block就是将元素显示为块级元素. block元素的特点是: 总是在新行上开始: 高度,行高以及顶和底边距都可控制: 宽度缺省是它的容器的100%,除非设定一个宽度 <div& ...
- [置顶]
Android ShareSDKQQ 第三方登录so easy?
昨天群里有个群友看到我之前做的那个qq第三方登录怎么做的,于是乎思考了一下,还是决定写一篇博客记录下,其实都不难的,其实之前我又写到FaceBook的第三方登录不知道看下这Android集成FaceB ...