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?的更多相关文章

  1. ORA-20000:ORU-10027:buffer overflow,limit of 2000 bytes.

     ORA-20000:ORU-10027:buffer overflow,limit of 2000 bytes.  这是因为在过程中用到了dbms_output.put_line()在服务器端输出信 ...

  2. ORA-20000 ORU-10027 buffer overflow limit of 2000 bytes

    这是在pl/sql中执行存储过程报的错,原因是serveroutput限制存储, 解决方案:set serveroutput on size 10000000;

  3. Windows 7 X64 SQL Server 2000 企业管理器无法建立新表

    问题现象: 当建立新表时会出现一个空白的窗口,敲击键盘输入后就会出现应用出错,然后退出. 解决方案: 选中某个已经存在的表,再点右键,新建表就可以正常操作了.

  4. node.js在windows下的学习笔记(8)---进程管理Process

    process是一个全局内置对象,可以在代码中的任何位置访问此对象,这个对象代表我们的node.js代码宿主的操作系统进程对象. 使用process对象可以截获进程的异常.退出等事件,也可以获取进程的 ...

  5. C10K问题渣翻译

    The C10K problem [Help save the best Linux news source on the web -- subscribe to Linux Weekly News! ...

  6. (转)The C10K problem翻译

    The C10K problem 如今的web服务器需要同时处理一万个以上的客户端了,难道不是吗?毕竟如今的网络是个big place了. 现在的计算机也很强大了,你只需要花大概$1200就可以买一个 ...

  7. 关于Socket 多线程 的一篇好文章

    http://www.kegel.com/c10k.html#topIt's time for web servers to handle ten thousand clients simultane ...

  8. [Oracle] UNIX与Windows 2000上Oracle的差异(III)

    作者:Ian Adam & David Stien, SAIC Ltd 日期:19-Dec-2003 出处:http://www.dbanotes.net翻译:Fenng ORACLE 的安装 ...

  9. WINDOWS操作系统中可以允许最大的线程数

      默认情况下,一个线程的栈要预留1M的内存空间 而一个进程中可用的内存空间只有2G,所以理论上一个进程中最多可以开2048个线程 但是内存当然不可能完全拿来作线程的栈,所以实际数目要比这个值要小.  ...

随机推荐

  1. SQLyog常用快捷键 - 转载

    SQLyog常用快捷键 1. SQL格式化 F12 格式化当前行所在的SQL Ctrl+F12    格式化选中的SQL 2. 窗口操作 Ctrl+T 打开一个新的查询窗口 Alt+L         ...

  2. WebUploader 解决文件多次上传和删除上传文件的问题

    文件多次上传有两种情况: 1. 上传前的多次选择 2. 上传成功后,再次选择 其实API上,已经有了介绍了,不知道为什么有同学还是不知道如何做,我来抛砖引玉吧. 配置项: duplicate {Boo ...

  3. Java通过JDBC操作Hive

    http://www.cnblogs.com/netbloomy/p/6688670.html 0.概述 使用的都是CLI或者hive –e的方式仅允许使用HiveQL执行查询.更新等操作.然而Hiv ...

  4. C语言查找算法之顺序查找、二分查找(折半查找)

    C语言查找算法之顺序查找.二分查找(折半查找),最近考试要用到,网上也有很多例子,我觉得还是自己写的看得懂一些. 顺序查找 /*顺序查找 顺序查找是在一个已知无(或有序)序队列中找出与给定关键字相同的 ...

  5. angular ng-repeat 如何实现嵌套

    slides是一个二维数组 <div ng-repeat="links in slides"> <div ng-repeat="link in link ...

  6. BZOJ 1492 [NOI2007]货币兑换Cash:斜率优化dp + cdq分治

    传送门 题意 初始时你有 $ s $ 元,接下来有 $ n $ 天. 在第 $ i $ 天,A券的价值为 $ A[i] $ ,B券的价值为 $ B[i] $ . 在第 $ i $ 天,你可以进行两种操 ...

  7. python学习笔记(datetime、字符串转换)

    datetime对象与字符串可以互相转化 代码如下: from datetime import datetime def datetime_string(time): return time.strf ...

  8. Kinect 2.0 默认姿势的中文意思

    RaiseRightHand/RaiseLeftHand 抬起左右手高于肩膀一秒Psi 举起双手高于肩膀一秒Tpose T姿势Stop 右手放下,左手缓慢贴住身侧(腰以下)或者左右调换Wave 挥手 ...

  9. Git GUI 的使用

    下面,我们开始使用Git Gui 如果你想init一个本地的git仓库,到你的代码根目录下,右键选择Git Init Here 这时,你会发现在代码根目录下,生成了一个.git的隐藏属性目录. 再选择 ...

  10. UVALive 4998 Simple Encryption

    题目描述: 输入正整数K1(K1<=5000),找一个12位正整数K2使得K1K2=K2(mod 1012). 解题思路: 压缩映射原理:设X是一个完备的度量空间,映射ƒ:Χ→Χ 把每两点的距离 ...