如何增加 Rust 库可用的堆栈大小? [英] How to increase the stack size available to a Rust library?

查看:72
本文介绍了如何增加 Rust 库可用的堆栈大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 lambda 演算,希望有更多的堆栈空间来构建和计算(非常)长的函数链.有没有办法为 crate 增加它,类似于增加递归限制 (#![recursion_limit = "100"])?

I am playing with lambda calculus and would like to have a bit more stack space to be able to build and compute (very) long function chains. Is there a way to increase it for the crate, similar to increasing the recursion limit (#![recursion_limit = "100"])?

crate 是一个库,我希望它能够执行堆栈密集型操作,而不管目标操作系统如何.

The crate is a library and I would like it to be able to perform stack-intensive operations regardless of the target operating system.

推荐答案

经过一些研究,我得出结论,没有一种通用的方法可以实现我所追求的目标,而是使用 std::thread::Builder 我能够创建一个具有指定堆栈大小的额外线程并在其中执行大量堆栈操作:

After some research I concluded that there isn't a universal way to achieve what I am after, but using std::thread::Builder I was able to create an extra thread with a specified stack size and perform stack-heavy operations inside it:

fn huge_reduction() {
    let builder = thread::Builder::new()
                  .name("reductor".into())
                  .stack_size(32 * 1024 * 1024); // 32MB of stack space

    let handler = builder.spawn(|| {
        // stack-intensive operations
    }).unwrap();

    handler.join().unwrap();
}

这篇关于如何增加 Rust 库可用的堆栈大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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