如何在编译期间设置线程堆栈大小? [英] How to set the thread stack size during compile time?

查看:64
本文介绍了如何在编译期间设置线程堆栈大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试运行构建大型clap::App<的程序时/code>(在这里找到源代码),我得到一个stackoverflow:thread '

'堆栈溢出.

When attempting to run a program that builds a large clap::App (find the source here), I get a stackoverflow: thread '<main>' has overflowed its stack.

到目前为止,我无法弄清楚如何指示 rustc 增加堆栈大小以进行暴力解决.RUST_MIN_STACK 似乎只适用于运行时,即使在那里它似乎也没有任何影响.

So far I was unable to figure out how to instruct rustc to increase the stacksize for a brute-force workaround. RUST_MIN_STACK seems to apply only to runtime, and even there it didn't seem to have any effect.

随着代码的生成,我可能需要做的是将 SubCommand 创建移动到运行时,这是我接下来要尝试的.

As the code is generated, what I would probably have to do is to move the SubCommand creation to the runtime, which is what I will try next.

但是,您是否看到了一种以不同方式解决此问题的方法?

解决这个问题似乎很重要,因为如果构建的结构足够大且嵌套足够多,构建器模式似乎很容易出现此问题.

git clone -b clap https://github.com/Byron/google-apis-rs
cd google-apis-rs
git checkout 9a8ae4b
make dfareporting2d1-cli-cargo ARGS=run

请注意,您将需要我的 quasi 分支,并在本地设置覆盖以允许使用最新的编译器进行构建.

Please note that you will need my fork of quasi and set an override locally to allow building with the latest compiler.

➜  google-apis-rs git:(clap) rustc --version
rustc 1.1.0-nightly (97d4e76c2 2015-04-27) (built 2015-04-28)

推荐答案

在 Rust 中没有办法设置主线程的堆栈大小.实际上,在 Rust 运行时库(https://github.com/rust-lang/rust/blob/master/src/libstd/rt/mod.rs#L85).

There's no way to set the stack size of the main thread in Rust. In fact, an assumption about the size of the main thread's stack is made at the source code level in the Rust runtime library (https://github.com/rust-lang/rust/blob/master/src/libstd/rt/mod.rs#L85).

环境变量 RUST_MIN_STACK 影响在程序中创建的线程的堆栈大小,而不是主线程,但您可以在运行时在源代码中轻松指定该值.

The environment variable RUST_MIN_STACK influences the stack size of threads created within the program that's not the main thread, but you could just as easily specify that value within your source code at runtime.

解决问题的最直接方法可能是在您创建的单独线程中运行 clap,以便您可以控制其堆栈大小.

The most straightforward way to solve your problem might be to run the clap in a separate thread you create, so that you can control its stack size.

以这段代码为例:

extern crate clap;
use clap::App;
use std::thread;

fn main() {
    let child = thread::Builder::new().stack_size(32 * 1024 * 1024).spawn(move || {
        return App::new("example")
            .version("v1.0-beta")
            .args_from_usage("<INPUT> 'Sets the input file to use'")
            .get_matches();
    }).unwrap();

    let matches = child.join().unwrap();

    println!("INPUT is: {}", matches.value_of("INPUT").unwrap());
}

clap 似乎能够从子线程中正确终止应用程序,因此您的代码应该只需稍加修改即可工作.

clap appears to be able to terminate the application correctly from within the child thread so your code should work with little modification.

这篇关于如何在编译期间设置线程堆栈大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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