struct - 在 Rust 中传递对结构的引用
问题描述
我的问题基本上是在我的程序中,我需要将对s
结构的引用传递到多个地方,包括一个新线程。例如,在 CI 中可以将其声明为全局结构并以这种方式使用它。我怎么能在生锈中做到这一点?
我还需要对一些代码使用RefCell
包装Rc
(我以前的问题)。
fn a_thread(s: &SomeStruct) {
//... code using s reference ... //
}
struct SomeStruct {
val: bool,
}
fn main() {
let mut s = SomeStruct { val: true };
let s_rc = Rc::new(RefCell::new(s));
thread::spawn(move || a_thread(&s)); // <= error: use of moved value 's'
//... code using the s_rc ... //
}
解决方案
如果一个线程修改数据而另一个线程读取它,则必须同步,否则您将遇到数据竞争。Safe Rust 通过静态分析防止数据竞争,因此它不会让您有一段&SomeStruct
时间可以通过另一个线程修改底层值。
您可以做的是使用互斥锁而不是RefCell
, 而Arc
不是Rc
:
fn a_thread(s: Arc<Mutex<SomeStruct>) {
// when you need data from s:
{
let s = s.lock().unwrap();
// here you can read from s, or even obtain a `&SomeStruct`
// but as long as you hold on to it, the main thread will be
// blocked in its attempts to modify s
}
}
fn main() {
// create s on the heap
let s = Arc::new(Mutex::new(SomeStruct { val: true }));
// cloning the Arc creates another reference to the value
let s2 = Arc::clone(&s);
thread::spawn(move || a_thread(s2));
//... code using s ... //
{
let s = s.lock().unwrap();
// here you can modify s, but reading will be blocked
}
}
推荐阅读
- oracle - Oracle Data Integrator-ODI 12.2.1--Loadplan Issue no of records count issue
- html - CSS“玻璃阴影”效果
- python - 如何获得全局步长变量的确定值(严格在增量之后或之前)
- dart - Flutter如何发布json数组
- amazon-web-services - Terraform 通过重新执行相同的 main.tf 创建多个 vpc
- javascript - 加载 AJAX 复选框的 Switchery 初始化错误
- amazon-web-services - 对其他 IAM 用户隐藏 EC2 实例
- .net - 为 ASP.NET Core lambda 函数 serverless.template 文件设置 AWS Authorizer 条目
- loops - Haskell:没有 (Num ()) 的实例 - 定义我自己的一元循环
- java - 设计 - 消费者模块的全局设置