How can I serialize sequential access to a resource in a threaded server application?

37 Views Asked by At

I'm writing a rust application that binds to a TCP/IP port and asynchronously spins up a stream handler for the connected sockets using tokio::spawn. I would like to handle multiple connections to the socket and serialize sequential access to a resource, say, I²C: the order of the sequences don't matter, but they should be atomic.

I'm not sure what mechanisms are available in rust for this sort of thing and was wondering if using lazy_static would my best course of action.

My thinking with lazy_static would be to declare a global variable for the bus, and have the spawned methods access the bus through that global static.

Does this approach make sense? As long as I don't have an .await in the methods that take ownership of the bus, I should be okay, no?

0

There are 0 best solutions below