Skip to content

help: capnp::message::Reader does not implement the Sync trait #191

Closed
@gjcarneiro

Description

@gjcarneiro

Hi, I am getting into an error trying to share capnp messages between threads.
Essentially, my data structure is HashMap<String, capnp::message::Reader<capnp::serialize::OwnedSegments>>. I am trying to protect it with Arc and RwLock (Arc::new(RwLock::new(...))), but still rust doesn't allow it.

It seems to be an error specific to capnp message reader. Other data types work fine. I don't understand why capnp::message::Reader doesn't implement Sync? AFAIK, a reader is just a view into a memory blob, with some methods to extract data. With the RwLock, it should be possible to read this data from multiple threads, and write from one.

   --> src/main.rs:419:5
    |
419 |     thread::spawn(move || {
    |     ^^^^^^^^^^^^^ `std::cell::Cell<u64>` cannot be shared between threads safely
    |
    = help: within `(std::string::String, capnp::message::Reader<capnp::serialize::OwnedSegments>)`, the trait `std::marker::Sync` is not implemented for `std::cell::Cell<u64>`
    = note: required because it appears within the type `capnp::private::arena::ReadLimiter`
    = note: required because it appears within the type `capnp::private::arena::ReaderArenaImpl<capnp::serialize::OwnedSegments>`
    = note: required because it appears within the type `capnp::message::Reader<capnp::serialize::OwnedSegments>`
    = note: required because it appears within the type `(std::string::String, capnp::message::Reader<capnp::serialize::OwnedSegments>)`
    = note: required because of the requirements on the impl of `std::marker::Sync` for `hashbrown::raw::RawTable<(std::string::String, capnp::message::Reader<capnp::serialize::OwnedSegments>)>`
    = note: required because it appears within the type `hashbrown::map::HashMap<std::string::String, capnp::message::Reader<capnp::serialize::OwnedSegments>, std::collections::hash_map::RandomState>`
    = note: required because it appears within the type `std::collections::HashMap<std::string::String, capnp::message::Reader<capnp::serialize::OwnedSegments>>`
    = note: required because it appears within the type `(std::string::String, std::collections::HashMap<std::string::String, capnp::message::Reader<capnp::serialize::OwnedSegments>>)`
    = note: required because of the requirements on the impl of `std::marker::Sync` for `hashbrown::raw::RawTable<(std::string::String, std::collections::HashMap<std::string::String, capnp::message::Reader<capnp::serialize::OwnedSegments>>)>`
    = note: required because it appears within the type `hashbrown::map::HashMap<std::string::String, std::collections::HashMap<std::string::String, capnp::message::Reader<capnp::serialize::OwnedSegments>>, 

[...]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions