一个进程(Process)最多可以生成多少个线程(Thread)
1.进程中创建线程的限制
默认情况下,一个线程的栈要预留1M的内存空间,而一个进程中可用的内存空间只有2G,所以理论上一个进程中最多可以开2048个线程,但是内存当然不可能完全拿来作线程的栈,所以实际数目要比这个值要小。
|
DWORD dwThreadId[MAX_THREADS]; hThread[i] = CreateThread(0,0, ThreadProc, 0, CREATE_SUSPENDED, &dwThreadId[i]); } } |
程序的运行结果是:
2.如何突破2000个限制?
你也可以通过连接时修改默认栈大小,将其改的比较小,这样就可以多开一些线程。 如将默认栈的大小改成512K,这样理论上最多就可以开4096个线程。
即使物理内存再大,一个进程中可以起的线程总要受到2GB这个内存空间的限制。比方说你的机器装了64GB物理内存,但每个进程的内存空间还是4GB,其中用户态可用的还是2GB。
如果是同一台机器内的话,能起多少线程也是受内存限制的。每个线程对象都要站用非页面内存,而非页面内存也是有限的,当非页面内存被耗尽时,也就无法创建线程了。
如果物理内存非常大,同一台机器内可以跑的线程数目的限制值会越来越大。
MSDN原文:
“The
number of threads a process can create is limited by the available
virtual memory. By default, every thread has one megabyte of stack
space. Therefore, you can create at most 2,048 threads. If you reduce the default stack size, you can create more threads.
However, your application will 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.”
可以通过修改CreateThread参数来缩小线程栈StackSize,例如(VC)
|
DWORD WINAPI ThreadProc(LPVOID lpParam) int main() { DWORD dwThreadId[MAX_THREADS]; if(0 == hThread[i]) } } |
注意上面红色带下划线变化的部分!(0==>512 * 1024,加上了STACK_SIZE_PARAM_IS_A_RESERVATION字段)
程序的运行结果是:
可以开启的线程数增长了一倍!!
服务器端程序设计
如果你的服务器端程序设计成:来一个client连接请求则创建一个线程,那么就会存在2000个限制(在硬件内存和CPU个数一定的情况下)。建议如下:
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.
1. Serve many clients with each thread, and use nonblocking I/O and level-triggered readiness notification
2. Serve many clients with each thread, and use nonblocking I/O and readiness change notification
3. Serve many clients with each server thread, and use asynchronous I/O
上面几句哈的核心的思想是:使用异步I/O,和一个线程处理多个客户请求!!
一个进程(Process)最多可以生成多少个线程(Thread)的更多相关文章
- windows 一个进程可以允许最大的线程数
默认情况下,一个线程的栈要预留1M的内存空间 而一个进程中可用的内存空间只有2G,所以理论上一个进程中最多可以开2048个线程 但是内存当然不可能完全拿来作线程的栈,所以实际数目要比这个值要小. 你也 ...
- 如何在Linux中统计一个进程的线程数(转)
方法一: /proc proc 伪文件系统,它驻留在 /proc 目录,这是最简单的方法来查看任何活动进程的线程数. /proc 目录以可读文本文件形式输出,提供现有进程和系统硬件相关的信息如 CPU ...
- (转)如何在Linux中统计一个进程的线程数
如何在Linux中统计一个进程的线程数 原文:http://os.51cto.com/art/201509/491728.htm 我正在运行一个程序,它在运行时会派生出多个线程.我想知道程序在运行时会 ...
- 如何在 Linux 中统计一个进程的线程数
编译自:http://ask.xmodulo.com/number-of-threads-process-linux.html作者: Dan Nanni原创:LCTT https://linux.cn ...
- 计算机必知必会:进程process与线程thread 进程定义为一个正在运行的程序的实例
http://www.nowamagic.net/librarys/veda/detail/1741进程和线程这对概念的理解也是很难的,至今网络上可查的资料对其的理解出入都挺大,在不同的操作系统中,如 ...
- 多并发编程基础 之进程 Process
原贴 https://www.cnblogs.com/gbq-dog/p/10299663.html 1. 进程的理论知识 1.1 操作系统的背景知识 顾名思义,进程即正在执行的一个过程.进程是对正 ...
- Python 进程(process)
1. 进程 1.1 进程的创建 fork 正在运行着的代码,就称为进程 # 示例: import os # 注意: fork 函数,只在 Unix/Linux/Mac 上运行, windows 不可以 ...
- nodeJS之进程process对象
前面的话 process对象是一个全局对象,在任何地方都能访问到它,通过这个对象提供的属性和方法,使我们可以对当前运行的程序的进程进行访问和控制.本文将详细介绍process对象 概述 process ...
- exit会结束一个进程
#include <stdio.h> #include <unistd.h> #include <sys/types.h> #include<stdlib.h ...
随机推荐
- LCA算法总结
LCA问题(Least Common Ancestors,最近公共祖先问题),是指给定一棵有根树T,给出若干个查询LCA(u, v)(通常查询数量较大),每次求树T中两个顶点u和v的最近公共祖先,即找 ...
- verilog语法实例学习(2)
Verilog中的信号类型 线网类型 线网类型表示一个或多个门或者其它类型的信号源驱动的硬件连线.如果没有驱动源,则线网的默认值为z.verilog中定义的线网类型有以下几种: wire,tr ...
- 在PC上像普通winform程序调试WINCE程序
在PC上像普通winform程序调试WINCE程序 步骤: 1. 在VS2008中到 工具→选项→设备工具→设备,选择对应的平台,另存为新的名称,如CEDesktopRun,关闭VS2008.(如果不 ...
- Nuget挂了的解决方法(转)
今天用Nuget下一个程序包时,发现Nuget挂了:未能解析此远程名称:'nuget.org'.第一反应就是方校长抖威风了,挂个代理上 http://nuget.org 试了下,果然好好的. 用命令n ...
- PLSQL Developer连接远程Oracle数据库
要连接远程数据库,传统的一定可行的方法是在本地装一个oracle.然后使用"Network Configuration Assistant"配置.之后用PL/SQL Dev连接.由 ...
- Graph 卷积神经网络:概述、样例及最新进展
http://www.52ml.net/20031.html [新智元导读]Graph Convolutional Network(GCN)是直接作用于图的卷积神经网络,GCN 允许对结构化数据进行端 ...
- CSS drop down的一个很好的示例
CSS drop down的一个很好的示例: http://www.runoob.com/css/css-dropdowns.html
- [Functional Programming] Arrow Functor with contramap
What is Arrow Functor? Arrow is a Profunctor that lifts a function of type a -> b and allows for ...
- JavaScript Array 对象常用方法
<script type="text/javascript"> //shift:删除原数组的第一项,返回删除元素的值:如果数组为空则返回undefined var ar ...
- Jquery——几个注意的小知识
event.stopPropagation() 停止事件冒泡 event.preventDefault()//组织默认行为(例如错误的时候,阻止按钮提交) event.type获取事件类型 event ...