gpt4 book ai didi

rust - 编译器表示数据不能在线程之间安全共享,即使数据被包裹在互斥体中

转载 作者:行者123 更新时间:2023-11-29 07:58:00 24 4
gpt4 key购买 nike

我正在使用具有 State 的 Rocket它传递给 HTTP 请求。这个结构包含一个 Mutex<DatastoreInstance>它提供对 SQLite 数据库的访问权限,并使用互斥锁锁定以确保读写安全。

pub struct DatastoreInstance {
conn: Connection,
}

DatastoreInstance结构看起来像这样,只有一个 SQLite 连接一切正常,但我还想在此结构中添加一个事务对象:

pub struct DatastoreInstance {
conn: Connection,
events_transaction: Transaction,
}

这没有编译因为 Transaction对象需要引用 Connection对象应该有一个它知道的生命周期。ConnectionTransaction我正在使用的 rusqlite 中的对象定义如下:

pub struct Connection {
db: RefCell<InnerConnection>,
cache: StatementCache,
path: Option<PathBuf>,
}

pub struct Transaction<'conn> {
conn: &'conn Connection,
drop_behavior: DropBehavior,
}

为了解决生命周期问题,我必须添加这些生命周期参数才能使其正常工作:

pub struct DatastoreInstance<'a> {
conn: Connection,
events_transaction: Transaction<'a>,
}

这就是结果,根据我对生命周期和互斥体的理解应该可以工作,但现在我收到一个编译器错误,告诉我:

`std::cell::RefCell<lru_cache::LruCache<std::string::String, rusqlite::raw_statement::RawStatement>>` cannot be shared between threads safely
|
= help: within `rusqlite::Connection`, the trait `std::marker::Sync` is not implemented for `std::cell::RefCell<lru_cache::LruCache<std::string::String, rusqlite::raw_statement::RawStatement>>`
= note: required because it appears within the type `rusqlite::cache::StatementCache`
= note: required because it appears within the type `rusqlite::Connection`
= note: required because of the requirements on the impl of `std::marker::Send` for `&rusqlite::Connection`
= note: required because it appears within the type `datastore::DatastoreInstance<'_>`
= note: required because of the requirements on the impl of `std::marker::Send` for `std::sync::Mutex<datastore::DatastoreInstance<'_>>`
= note: required because it appears within the type `endpoints::ServerState<'_>`
= note: required by `rocket::State`

根据我对互斥锁的理解,这段代码应该是有效的,因为整个DatastoreInstance结构包裹在 Mutex 中这应该保证一次只有一个线程引用该对象。

我错过了什么?

为什么编译器找不到RefCellConnection 内后就不再安全了在 Transaction 中引用而不是仅在 Connection 内?

我对互斥锁的工作原理理解不好吗?我的生命周期是否无效并且以某种方式破坏了读/写安全性?是有 Connection 的设计和 Transaction在同一个结构中,一个糟糕的设计会破坏读/写安全性?我是否需要以某种方式重新设计我的数据结构以使其安全?还是我只是漏掉了一些非常明显的东西?

最佳答案

Mutex 只是SendSync if the value it contains is itself Send :

impl<T: ?Sized + Send> Send for Mutex<T>    
impl<T: ?Sized + Send> Sync for Mutex<T>

&T 只是发送 when T is Sync :

impl<'a, T> Send for &'a T
where
T: Sync + ?Sized,

还有一个RefCell is never Sync

impl<T> !Sync for RefCell<T>
where
T: ?Sized,

如错误消息所述,您的交易包含对 RefCell 的引用。有互斥量并不重要,跨线程共享它本质上不是内存安全的。一个简单的复制:

use std::{cell::RefCell, sync::Mutex};

struct Connection(RefCell<i32>);
struct Transaction<'a>(&'a Connection);

fn is_send<T: Send>(_: T) {}

fn main() {
let c = Connection(RefCell::new(42));
let t = Transaction(&c);
let m = Mutex::new(t);

is_send(m);
}
error[E0277]: `std::cell::RefCell<i32>` cannot be shared between threads safely
--> src/main.rs:13:5
|
13 | is_send(m);
| ^^^^^^^ `std::cell::RefCell<i32>` cannot be shared between threads safely
|
= help: within `Connection`, the trait `std::marker::Sync` is not implemented for `std::cell::RefCell<i32>`
= note: required because it appears within the type `Connection`
= note: required because of the requirements on the impl of `std::marker::Send` for `&Connection`
= note: required because it appears within the type `Transaction<'_>`
= note: required because of the requirements on the impl of `std::marker::Send` for `std::sync::Mutex<Transaction<'_>>`
note: required by `is_send`
--> src/main.rs:6:1
|
6 | fn is_send<T: Send>(_: T) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^

Why doesn't the compiler find RefCell to be safe anymore after being within a Connection referenced within a Transaction instead of solely within a Connection?

RefCell 很好,它是对 RefCell引用,但不是。

Is the design of having the Connection and Transaction within the same struct a bad design [...] Do I need to redesign my data structures

是的。

关于rust - 编译器表示数据不能在线程之间安全共享,即使数据被包裹在互斥体中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52801244/

24 4 0