我将 Rust 视为硬实时编程中 C/C++ 的替代品。我发现了两个可能的问题:
1) 如何避免调用 Rust 的 GC?我看到有人建议我可以通过简单地避免托管指针和非实时安全库(例如 Rust 的标准库)来做到这一点——这是否足以保证我的实时任务永远不会调用 GC?
2) 如何将实时任务映射到操作系统线程?我知道 Rust 的标准库实现了 N:M 并发模型,但实时任务必须直接对应一个操作系统线程。有没有办法生成这种类型的线程?
最佳答案
1) How to I avoid invoking Rust's GC? I've seen suggestions that I can do this by simply avoiding managed pointers and non-realtime-safe libraries (such as Rust's standard library) -- is this enough to guarantee my realtime task will never invoke the GC?
是的,避开@
将避免GC。 (Rust 目前实际上并没有实现 GC,所以现在所有代码都会自动避免它。)
2) How do I map my realtime task to an OS thread? I know Rust's standard library implements an N:M concurrency model, but a realtime task must correspond directly with one OS thread. Is there a way to spawn a thread of this type?
<a href="http://static.rust-lang.org/doc/master/std/task/fn.spawn_sched.html" rel="noreferrer noopener nofollow">std::task::spawn_sched</a>(<a href="http://static.rust-lang.org/doc/master/std/task/enum.SchedMode.html" rel="noreferrer noopener nofollow">std::task::SingleThreaded</a>, function)
(特殊格式将在 #10095 登陆时修复),例如
use std::task;
fn main() {
do task::spawn_sched(task::SingleThreaded) {
println("on my own thread");
}
}
也就是说,Rust 的运行时和标准库还没有为硬实时编程设置(还),但您可以使用 #[no_std]
运行“无运行时” ( example ) 它给你的情况与 C/C++ 完全相同,模数语言差异和缺少标准库(尽管 Rust's FFI 意味着你可以相对容易地调用 libc,并且 rust-core 项目被设计成为一个最小的标准库,甚至不需要 libc 就可以工作)。
关于real-time - 我如何在 Rust 中进行实时编程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19612801/