Restricting objects to a thread pool


#1

I have some FFI objects that are thread safe, but only within a specific thread pool. Every thread that wants to share these objects with each other needs to register to become included in this pool.

What would be the Rust way to model this restricted sharing? I.e. so that an object can easily be shared within the pool but not to threads outside of it?


#2

Simply make it !Send. If clones of an object woudn’t make sense outside of the source object’s thread, make it !Sync too.

Send on a type means that it makes sense to move values of that type between threads. Sync on a type means that it makes sense to clone values of that type from a different thread.

Edit: somewhat misread, see subsequent posts for correction


#3

But what I’m trying to say is that in this case it makes sense to send to some threads but not other threads, if you see what I mean?


#4

Ah. Here’s an idea.

Have a channel type whose initialization is restricted to creation between threads which can be proven to be in the same threadpool. When a threadpool-local value (which is otherwise !Send) should be sent to another thread, it is sent through this channel, and, in a vein similar to stdlib’s MPSC channel, either it makes it through successfully, or the other end already hung up or is unavailable, and the value is bounced back to its sender.


#5

Ahh, thanks. So I’ll crib from the general sender/receiver API from mpsc but will be more careful about which threads can hold the channel.