Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Struct 传递对Rust中结构的引用_Struct_Rust_Reference_Refcell - Fatal编程技术网

Struct 传递对Rust中结构的引用

Struct 传递对Rust中结构的引用,struct,rust,reference,refcell,Struct,Rust,Reference,Refcell,我的问题基本上是,在我的程序中,我需要将对s结构的引用传递到多个地方,包括一个新线程。例如,在C语言中,我可以将它声明为一个全局结构并以这种方式使用它。 我怎么能在生锈的地方这样做 我还需要使用RefCell包装在Rc中的一些代码(我前面的问题) fn一个线程(s:&SomeStruct){ //…使用s引用的代码// } 结构SomeStruct{ 瓦尔:布尔, } fn main(){ 设muts=SomeStruct{val:true}; 设s_rc=rc::new(RefCell::ne

我的问题基本上是,在我的程序中,我需要将对
s
结构的引用传递到多个地方,包括一个新线程。例如,在C语言中,我可以将它声明为一个全局结构并以这种方式使用它。 我怎么能在生锈的地方这样做

我还需要使用
RefCell
包装在
Rc
中的一些代码(我前面的问题)

fn一个线程(s:&SomeStruct){
//…使用s引用的代码//
}
结构SomeStruct{
瓦尔:布尔,
}
fn main(){
设muts=SomeStruct{val:true};
设s_rc=rc::new(RefCell::new(s));

thread::spawn(move | | | a_thread(&s))/如果一个线程修改数据,另一个线程读取数据,则必须同步,否则会发生数据争用。安全锈迹通过静态分析防止数据争用,因此它不允许您获得
&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
    }
}

fn a_线程(s:Arc如果一个线程修改数据,另一个线程读取数据,则必须进行同步,否则将存在数据争用。安全锈迹通过静态分析防止数据争用,因此它不允许您获得
&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
    }
}

fn a_线程(s:ArcDoes
a_线程()
需要修改
s
?获取共享引用实际上可以吗?此外,所示代码也不适用于单线程代码(
Rc
RefCell
),因为您试图将
s
移动到闭包中,而不是
s_Rc
a_线程()
只需要从结构中读取值。我想从主线程修改结构是否需要修改
a_thread()
是否需要修改
s
?是否可以使用共享引用?此外,所示代码也不能与单线程代码一起使用(
Rc
RefCell
)因为您试图将
s
移动到闭包中,而不是
s\u rc
a\u线程()
只需要从结构中读取值。我想从主线程修改结构,因为只有一个线程需要修改值,所以RwLock在这里不是更好吗?@RedBorg它的工作原理是一样的,因为读写器仍然会相互锁定。上次我测量时,获得一个无争议的
RwLock
由于簿记更为复杂,因此比获取
互斥锁
更为昂贵。
RwLock
应在实际提供好处时使用:如果数据位于
互斥锁
@user4815162342后面,则多个读卡器会相互锁定。当然,对于单个线程来说,成本会更高。我以为会有多个线程都在监视同一个值,我的错。既然只有一个线程需要修改值,那么RwLock在这里不是更好吗?@RedBorg它的工作原理是一样的,因为读写器仍然会相互锁定。上次我测量时,获得一个无竞争的
RwLock
比获取一个
互斥锁
,因为它的簿记更加复杂。当它真正提供好处时,应该使用
rBlock
:当有多个读卡器在
互斥锁
@user4815162342后面时,如果数据在
互斥锁
,那么当然,对于一个线程来说,它的成本会更高。我认为会有可能是多个线程都在看同一个值,我的错。