简体   繁体   English

如何在Rust中进行实时编程?

[英]How do I do realtime programming in Rust?

I'm looking at Rust as a replacement for C/C++ in hard realtime programming. 我正在寻找Rust作为硬实时编程中C / C ++的替代品。 There are two possible issues I've identified: 我发现了两个可能的问题:

1) How to I avoid invoking Rust's GC? 1)如何避免调用Rust的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? 我已经看到一些建议,可以通过简单地避免使用托管指针和非实时安全的库(例如Rust的标准库)来做到这一点-是否足以确保我的实时任务永远不会调用GC?

2) How do I map my realtime task to an OS thread? 2)如何将我的实时任务映射到OS线程? I know Rust's standard library implements an N:M concurrency model, but a realtime task must correspond directly with one OS thread. 我知道Rust的标准库实现了N:M并发模型,但是实时任务必须直接与一个OS线程相对应。 Is there a way to spawn a thread of this type? 有没有办法产生这种类型的线程?

1) How to I avoid invoking Rust's GC? 1)如何避免调用Rust的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? 我已经看到一些建议,可以通过简单地避免使用托管指针和非实时安全的库(例如Rust的标准库)来做到这一点-是否足以确保我的实时任务永远不会调用GC?

Yes, avoiding @ will avoid the GC. 是的,避免@将避免GC。 (Rust currently doesn't actually have the GC implemented, so all code avoids it automatically, for now.) (目前,Rust实际上尚未实现GC,因此,所有代码现在都自动避免使用它。)

2) How do I map my realtime task to an OS thread? 2)如何将我的实时任务映射到OS线程? I know Rust's standard library implements an N:M concurrency model, but a realtime task must correspond directly with one OS thread. 我知道Rust的标准库实现了N:M并发模型,但是实时任务必须直接与一个OS线程相对应。 Is there a way to spawn a thread of this type? 有没有办法产生这种类型的线程?

std::task::spawn_sched ( std::task::SingleThreaded , function) (the peculiar formatting will be fixed when #10095 lands), eg std::task::spawn_sched ( std::task::SingleThreaded , function) (特殊格式会在#10095着陆时被固定),例如

use std::task;
fn main() {
    do task::spawn_sched(task::SingleThreaded) {
        println("on my own thread");
    }
}

That said, Rust's runtime & standard libraries aren't set up for hard -realtime programming (yet), but you can run "runtimeless" using #[no_std] ( example ) which gives you exactly the same situation as C/C++, modulo language differences and the lack of a standard library (although Rust's FFI means that you can call into libc relatively easily, and the rust-core project is designed to be a minimal stdlib that doesn't even require libc to work). 也就是说,Rust的运行时和标准库尚未设置用于实时编程(但),但是您可以使用#[no_std]示例 )运行“ runtimeless”,这将为您提供与C / C ++完全相同的情况,模语言差异和缺乏标准库(尽管Rust的FFI意味着您可以相对轻松地调用libc,并且rust-core项目被设计为最小的stdlib,甚至不需要libc即可工作)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM