在并行循环 C# 中定义堆栈大小

Define stack size in a parallel for loop C#

我正在实施随机森林算法。每棵树都以递归方式训练(随着树变深,调用堆栈的大小增加),我可以毫无问题地训练一棵树(或顺序循环中的几棵树)。

然而,在 Parallel.For 循环中训练所有树会导致堆栈溢出。我知道在使用 new Thread() 时可以配置堆栈大小,如下所述:How to change stack size for a .NET program?

但是,可以用 Parallel.For 来做吗?或者我是否必须编写所有线程,指定它们的堆栈大小?

来自social.msdn

By default, TPL uses threads from the CLR ThreadPool. The stack size for a ThreadPool thread is the process' default stack size, which is determined by the executable file containing the process entry point.

You can change the default stack size for the process by editing this setting in the executable. One way to do this is with "editbin.exe," which comes with Vistual Studio. If your executable is "myprogram.exe" and you want a 2MB stack, you might run:

editbin /stack:2097152 myprogram.exe

Hope that helps!