增加主程序的堆栈大小还是为递归代码块创建具有更大堆栈大小的新线程? [英] Increase stack size of main program or create a new thread with larger stack size for recursive code blocks?
问题描述
我应该使用以下后期构建事件来增加主程序的堆栈大小:
Should I increase the stack size of my main program using the following post build event:
"$(DevEnvDir)..\..\VC\bin\editbin.exe" /STACK:8388608 "$(TargetPath)"
还是应该将递归代码块封装在具有更大堆栈大小的新线程中?
or should I capsule my recursive code block inside a new thread with a larger stack size?
Thread thread = new Thread(delegate()
{
// do work with larger stack size
}, 8192 * 1024);
thread.Start();
thread.Join();
具有很多递归的代码来自Intel MKLs LAPACKE_dtrtri 函数,我通过 DLLImport $ c $调用了该函数c>。因此,我无法更改此代码。我可以更改堆栈大小以避免堆栈溢出错误。
The code which has a lot of recursion is from Intel MKLs the LAPACKE_dtrtri function which I call over DLLImport
. So I cannot change this code. I can just change the stack size to avoid a stack overflow error.
-
分配更大堆栈大小的缺点是什么
What is the draw back of assign a larger stack size to my main program?
为该计算创建具有更大堆栈大小的新线程的缺点是什么?
What is the draw back of creating a new thread with a larger stack size for this calculation?
哪个解决方案更好?
合理的堆栈大小是多少? 8 MB,16 MB,32 MB或64 MB(例如 MATLAB )?该程序在具有至少16 GB RAM(最大256 GB)的计算机上运行。
What would be a reasonable stack size? 8 MB, 16 MB, 32 MB or 64 MB (like MATLAB)? The program runs on computers which have at least 16 GB of RAM (up to 256 GB).
注意:我的应用程序有99%的默认堆栈大小为4 MB(64位)就足够了,但其他1%的用户使用的是高度递归的外部代码。
Note: for 99% of my application the default stack size of 4 MB (64-bit) would be enough but the other 1% is using external code which is heavily recursive.
推荐答案
- 哪个解决方案更好?
- 哪个解决方案更好?
第一种方法使用 editbin $使用强名称密钥对程序集进行签名时,后生成事件中的c $ c>失败。使用
editbin
更改了程序集后,对签名程序集的验证将失败。 sn.exe -v assembly.exe
将返回验证程序集失败-强名称验证失败...
The first approach using editbin
in a post-build event is failing when using strong name key to sign the assembly. After the assembly was changed using editbin
the verification of the signed assembly will fail. sn.exe -v assembly.exe
will return Failed to verify assembly -- Strong name validation failed ...
另请参见:
- Is an assembly signed with a strong name before or after the post-build event?
使用 AfterCompile
事件和辞职大会是一种解决方法(我现在正在使用)。项目文件应包含以下行:
Using the AfterCompile
event and resigning the assembly is a workaround (which I'm using now). The project file should contain the following lines:
<Target Name="AfterCompile">
<Exec Command="
"$(DevEnvDir)..\..\VC\bin\editbin.exe" /STACK:16777216 "$(ProjectDir)obj\$(ConfigurationName)\$(TargetFileName)"
"$(FrameworkSDKDir)bin\NETFX 4.5.1 Tools\sn.exe" -Ra "$(ProjectDir)obj\$(ConfigurationName)\$(TargetFileName)" "$(SolutionDir)\STRONGNAME.snk"
" />
</Target>
<PropertyGroup>
<PostBuildEvent>REM "See AfterCompile for stack size and resigning"</PostBuildEvent>
</PropertyGroup>
在阅读以下答案时,我意识到了编译后事件:https://stackoverflow.com/a/22617361/7556646
I was getting aware of the after compile event when I was reading the following answer: https://stackoverflow.com/a/22617361/7556646
第二种方法,但对于漏洞程序,不仅对于递归代码块,都将如下所示:
The second approach but for the hole program and not only for the recursive code blocks would look like that:
static class Program
{
[STAThread]
static void Main(string[] args)
{
Thread thread = new Thread(delegate()
{
Main2(args);
}, 16 * 1024 * 1024);
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
}
static void Main2(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(app);
}
}
第二种方法的缺点是BackgroundWorker DoWork事件仍然是1 MB(32位或任意位)或4 MB(64位)。
The second approach has the drawback that the stack size of a BackgroundWorker DoWork event is still 1 MB (32-bit or any) or 4 MB (64-bit).
另请参见:
- What is the stack size of a BackgroundWorker DoWork Thread? Is there way to change it?
- 什么是为该计算创建具有更大堆栈大小的新线程的缺点?
- 合理的堆栈大小是多少?
请参阅Hans Passant的评论。
See the comments from Hans Passant.
这篇关于增加主程序的堆栈大小还是为递归代码块创建具有更大堆栈大小的新线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!