Need help regarding Send and Sync

Hi, I have a library containing a data-structure, you can think of it as an in-memory database. This data-structure can be safely accessed from multiple threads and I require all types stored to be Send + Sync. Each stored type has it's own Vec-like storage. To make any operation, you first request the storage and then can access data through a guard.

Recently I had an idea to allow !Send and !Sync types but I'm not 100% it's correct.

  • Send + !Sync -> shared and unique access in one thread at a time
  • !Send + Sync -> shared access only
  • Send + !Sync -> shared and unique access in the thread it was added in
  • The data-structure have to loose Send

The guard would have the same bound as the storage.

Are these guaranties enough?

Thank you for your time :smiley:

EDIT: mutable access is also ok for Send + !Sync
EDIT2: add !Send + Sync case

Yes, the restrictions you propose appear to be safe. Additionally:

Mutable access on one thread at a time would also be safe here.


Yes, it's the same as Send + Sync so in my head it's not a limitation on !Sync but you're absolutely right.

In the meantime !Send + Sync is sitting in the corner :stuck_out_tongue:

  • for that case, you can authorize non-&mut nor owned access to the value

Be careful not to drop the !Send on any other thread too.

1 Like

@Yandros I thought Clone for example was an issue but it's not the same value in the end and swap asks for &mut, so shared references only but in multiple threads.

@jonh Oh good catch, so the data-structure has to be !Send... that's not very nice but I'll live with it.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.