Reputation: 325
I'm using uWebSockets version 0.15.x to accept and manager web socket connections on a server. I have one thread calling uWS::TemplatedApp::run to handle this.
I need to send data to all open web sockets from another thread. My understanding is that uWebSockets is not thread safe. What is the recommended way to do this?
(The uWebSockets user manual for 0.14.x discusses uS::Async, but this class is gone in 0.15.x. It is not clear from the current documentation what the equivalent is.)
Upvotes: 2
Views: 1609
Reputation: 325
This can be done using the following methods:
In the proposed use case, the "server" thread can call uWS::Loop::defaultLoop and provide that pointer to other threads. Other threads can call uWS::Loop::defer to run code in the "server" thread, capturing required data in a lambda. The threads can share a list of sockets as long as it's either multi-thread safe, or only accessed from code run in the "server" thread.
Upvotes: 1