编译器表示,即使数据包含在Mutex中,也无法安全地在线程之间共享数据

Joh*_*olt 3 mutex thread-safety rust rust-rocket

我正在使用Rocket State,它传递给HTTP请求.此结构包含一个Mutex<DatastoreInstance>访问SQLite数据库的框架,并使用互斥锁锁定以使读写安全.

pub struct DatastoreInstance {
    conn: Connection,
}
Run Code Online (Sandbox Code Playgroud)

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

pub struct DatastoreInstance {
    conn: Connection,
    events_transaction: Transaction,
}
Run Code Online (Sandbox Code Playgroud)

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

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

pub struct Transaction<'conn> {
    conn: &'conn Connection,
    drop_behavior: DropBehavior,
}
Run Code Online (Sandbox Code Playgroud)

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

pub struct DatastoreInstance<'a> {
    conn: Connection,
    events_transaction: Transaction<'a>,
}
Run Code Online (Sandbox Code Playgroud)

这是结果,并且应该根据我对生命周期和互斥体的理解而工作,但是我现在得到编译器错误告诉我:

`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`
Run Code Online (Sandbox Code Playgroud)

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

我错过了什么?

为什么编译器RefCell在a Connection内被引用Transaction而不是仅仅在一个内部之后才发现它是安全的Connection

我对互斥体的工作方式有不了解吗?我的生命是否无效并以某种方式打破了读/写安全性?具有ConnectionTransaction在同一结构内的设计是否设计会破坏读/写安全性?我是否需要以某种方式重新设计我的数据结构以确保安全?或者我只是遗漏了一些非常明显的东西?

She*_*ter 5

A Mutex只是Send或者Sync 它包含的值本身是Send:

impl<T: ?Sized + Send> Send for Mutex<T>    
impl<T: ?Sized + Send> Sync for Mutex<T>
Run Code Online (Sandbox Code Playgroud)

A &TSend 在以下T时间Sync:

impl<'a, T> Send for &'a T
where
    T: Sync + ?Sized, 
Run Code Online (Sandbox Code Playgroud)

而且RefCell 永远不会Sync

impl<T> !Sync for RefCell<T>
where
    T: ?Sized, 
Run Code Online (Sandbox Code Playgroud)

正如错误消息所述,您的事务包含对a的引用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);
}
Run Code Online (Sandbox Code Playgroud)
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) {}
   | ^^^^^^^^^^^^^^^^^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)

为什么编译器RefCell在a Connection内被引用Transaction而不是仅仅在一个内部之后才发现它是安全的Connection

RefCell是好的,它是参考RefCell,是不是.

是具有设计ConnectionTransaction相同的结构设计不好[...]我需要重新设计我的数据结构中

是.