CreateThread 的保留和提交参数有什么区别? [英] What is the Difference between reserve and commit argument to CreateThread?

查看:19
本文介绍了CreateThread 的保留和提交参数有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CreateThread Windows API 函数的保留参数和提交参数有什么区别?

What is the difference between reserve argument and commit argument to CreateThread Windows API function?

我无法理解以下几行..

I can't understand the following lines ..

reserve 参数设置系统应该为线程堆栈保留的地址空间量.默认值为 1 MB.commit 参数指定最初应提交到堆栈保留区域的物理存储量.

The reserve argument sets the amount of address space the system should reserve for the thread's stack. The default is 1 MB. The commit argument specifies the amount of physical storage that should be initially committed to the stack's reserved region.

您将在本段中找到这两行,它解释了 C++ 中 CreateThread 函数的参数之一

these two lines you will find them in this paragraph which explains one of the parameters of the CreateThread function in c++

cbStackSize

cbStackSize 参数指定线程可以为自己的堆栈使用多少地址空间.每个线程都有自己的堆.当CreateProcess 启动一个进程时,它会在内部调用CreateThread 来初始化进程的主线程.为了cbStackSize 参数,CreateProcess 使用存储在可执行文件中的值.您可以使用链接器的 /STACK 开关:

The cbStackSize parameter specifies how much address space the thread can use for its own stack. Every thread owns its own stack. When CreateProcess starts a process, it internally calls CreateThread to initialize the process' primary thread. For the cbStackSize parameter, CreateProcess uses a value stored inside the executable file. You can control this value using the linker's /STACK switch:

/STACK:[reserve][, commit]

reserveargument 设置系统应该为线程堆栈保留的地址空间量.默认值为 1 MB.commitargument 指定最初应提交给堆栈保留区域的物理存储量.

The reserveargument sets the amount of address space the system should reserve for the thread's stack. The default is 1 MB. The commitargument specifies the amount of physical storage that should be initially committed to the stack's reserved region.

推荐答案

区别在于虚拟内存和物理内存的区别.

The distinction is distinction between virtual and physical memory.

在任何名副其实的操作系统中,包括 Windows,指针不直接指定内存芯片上的位置.它们是特定于进程的虚拟内存空间中的位置,然后操作系统分配物理内存芯片的部分来存储进程实际按需存储任何内容的部分的内容.并且可能会在内存不足时将一些数据交换到磁盘.

In any operating system worthy of that name, including Windows, pointers don't designate locations on the memory chip directly. They are locations in process-specific virtual memory space and the operating system then allocates parts of the physical memory chip to store the content of the parts where the process actually stores anything on demand. And may swap some data to disk when it runs out of RAM.

reserve 是分配给堆栈的连续虚拟内存块的大小.低于和高于范围的其他东西将被存储,因此储备对堆栈可以增长的大小设置了上限.

The reserve is the size of continuous virtual memory block to allocate for the stack. Below and above the range other things will be stored, so the reserve puts upper limit on how big the stack can grow.

幸运的是,虚拟内存通常很充足.您在 32 位 Windows 上有 2GiB,如果您使用 /LARGEADDRESSAWARE 标志链接,则为 3GiB,如果您为 64 位 (x64) 编译,则数量巨大.唯一的例外是 5.0 之前的 WinCE,您只有 32MiB.因此,除非您正在创建无数个线程,否则您可以在这里慷慨解囊,也应该如此,因为如果没有足够的线程,进程就会崩溃.

Fortunately virtual memory is usually plentiful. You have 2GiB on 32-bit Windows, 3GiB if you link with /LARGEADDRESSAWARE flag and huge amount if you compile for 64-bits (x64). Only exception is WinCE before 5.0, where you only have 32MiB. So unless you are creating zillions of threads, you can be generous here and you should be, because if you don't have enough, the process will crash.

commit 是系统应该为堆栈预分配的物理内存大小.这使得系统立即在物理内存中获得一些空间,该空间是共享资源并且可能是稀缺的.它可能需要交换或丢弃它以前的内容才能获得它.当您超过它时,系统会以较小的延迟为代价自动加扰一些.因此,通过增加此处的值获得的唯一好处是如果您确实需要内存,则可以稍微加快速度.如果你不这样做,它会变慢.所以在这里你应该保守一点.

The commit is the size of physical memory that the system should preallocate for the stack. This makes the system immediately get some space in the physical memory, which is a shared resource and may be scarce. It may need to swap or discard it's previous content to get it. When you exceed it, the system will automatically scramble some more at the cost of small delay. So the only thing you gain by increasing the value here is a little speed-up if you actually have need for the memory. It's slow down if you don't. So you should be conservative here.

栈是放置局部变量的地方.如果您使用大型本地缓冲区—这通常是合理的,因为堆栈分配比堆分配快得多(通过 malloc/new/任何使用 std::allocator)—你需要预留足够的堆栈.如果不这样做,1MiB 通常就足够了.

The stack is where local variables are placed. If you use large local buffers—and it is often reasonable since stack allocation is much faster than heap allocation (via malloc/new/anything that uses std::allocator)—you need to reserve enough stack. If you don't, the 1MiB is usually plenty.

这篇关于CreateThread 的保留和提交参数有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆