博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
一个进程(Process)最多可以生成多少个线程(Thread)
阅读量:6312 次
发布时间:2019-06-22

本文共 3138 字,大约阅读时间需要 10 分钟。

 

1.进程中创建线程的限制

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

#define MAX_THREADS 50000

#include <Windows.h>
#include <stdio.h>
DWORD WINAPI ThreadProc(LPVOID lpParam)
{
    while(1)
    {
        Sleep(100000);
    }
    return 0;
}
int main() {
    DWORD dwThreadId[MAX_THREADS];
    HANDLE hThread[MAX_THREADS];
    void * stack[MAX_THREADS];
    for(int i = 0; i < MAX_THREADS; ++i)
    {
  hThread[i] = CreateThread(0,0, ThreadProc, 0, CREATE_SUSPENDED, &dwThreadId[i]);
        if(0 == hThread[i])
        {
            DWORD e = GetLastError();
            if (e == 8)
            {
                printf("Out of Memory!\n",e);
            }
            else
            {
                printf("%d\r\n",e);
            }
            break;
        }
        else
        {
            printf("%d:%d\r\n",i,hThread[i]);
        }
    }
    ThreadProc(0);
}

 

程序的运行结果是:

【  】
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)

#define MAX_THREADS 50000

#include <Windows.h>
#include <stdio.h>
DWORD WINAPI ThreadProc(LPVOID lpParam)
{
    while(1)
    {
        Sleep(100000);
    }
    return 0;
}
int main() {
    DWORD dwThreadId[MAX_THREADS];
    HANDLE hThread[MAX_THREADS];
    void * stack[MAX_THREADS];
    for(int i = 0; i < MAX_THREADS; ++i)
    {
        hThread[i] = CreateThread(0,512 * 1024, ThreadProc, 0, STACK_SIZE_PARAM_IS_A_RESERVATION | CREATE_SUSPENDED, &dwThreadId[i]);
        if(0 == hThread[i])
        {
            DWORD e = GetLastError();
            if (e == 8)
            {
                printf("Out of Memory!\n",e);
            }
            else
            {
                printf("%d\r\n",e);
            }
            break;
        }
        else
        {
            printf("%d:%d\r\n",i,hThread[i]);
        }
    }
    ThreadProc(0);
}        

注意上面红色带下划线变化的部分!(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,和一个线程处理多个客户请求!!

转载地址:http://izexa.baihongyu.com/

你可能感兴趣的文章
Oracle 11g password过期被锁定报道 ORA-28000 the account is locked
查看>>
【Struts2学习笔记(2)】Action默认值和配置Action于result各种转发类型
查看>>
轨磁条简介
查看>>
(算法)交错的字符串
查看>>
hdu 5471(状压DP or 容斥)
查看>>
oracle.jdbc.driver.OracleDriver和oracle.jdbc.OracleDriver这两个驱动的区别
查看>>
NSQ部署
查看>>
git常用命令记录
查看>>
IBM发布新一代云计算工具包MobileFirst Foundation
查看>>
唯品会HDFS性能挑战和优化实践
查看>>
大规模学习该如何权衡得失?解读NeurIPS 2018时间检验奖获奖论文
查看>>
大厂前端高频面试问题与答案精选
查看>>
我们用5分钟写了一个跨多端项目
查看>>
Visual Studio 15.4发布,新增多平台支持
查看>>
有赞透明多级缓存解决方案(TMC)设计思路
查看>>
如何设计高扩展的在线网页制作平台
查看>>
Git 2.5增加了工作树、改进了三角工作流、性能等诸多方面
查看>>
Swift 5将强制执行内存独占访问
查看>>
中台之上(二):为什么业务架构存在20多年,技术人员还觉得它有点虚?
查看>>
深度揭秘腾讯云低功耗广域物联网LPWAN 技术及应用
查看>>