Use Vector between threads without locking it

I am currently working on a project where I have a Mutex<Vec<T>> in a lazy_static block.
I have a multithreaded websocket system that access to this Vec. There are now so many request that is it always locked so connection are not able to access it.
I have got ideas to resolve this problem but I don't know how to implement them in Rust:

  • Have a thread that manage the Vector
  • Have a lockable array for editing it and a shared for reading
  • Have a sort of mutex but that can read without locking or allow multiple read instance at the same time

(There are around 2000 locks per second and a third lock the array)

Thank's by advance for helping me.

Perhaps you want a RwLock instead?

I am going to try. Thank's alice.

Good luck. If that doesn't work, you'll need a thread-safe vector, that either uses locking internally (on a finer-grained level) or is lock-free. In both cases, I strongly recommend searching for existing libraries and not implement them yourself. It's easy to cause deadlocks (locking version) or UB (lock-free version), if you do it from scratch.

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