Question about websocket read/writes thread safety. #1942
-
HI, I'm trying to figure out if it's thread safe to call mg_ws_write from a completely different thread than the thread doing the reads. I read through a bunch of issues/questions here and my guess so far is that this is fine. It seems that the websocket send events fire on the poll event. Presumably, as long as I'm mg_ws_write ing from the same thread, I don't think it could have any async issues. However, then I saw this comment:
If a read takes say, 2 milliseconds, I believe this would delay the actual tcp packet sending from my mg_ws_write by 2 milliseconds due to packet writes waiting on the poll event to be processed. I saw another issue here about waking up the manager in order to perform an immediate write but the referenced wakeup function no longer seems to exist. Is there a way to wake up the manager to immediately send a ws packet without waiting on the next poll? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
From the docs
If you absolutely need multi-threading, take the time to go through the user guide (linked above) and tutorials, there is one that shows you how to do multi-threading. Please read the official docs instead of comments/issues from 2013, 10 years ago. Mongoose is 7.8 now Your OS provides a socket environment and a select()/poll() call. Mongoose will wait on those sockets, so reads are not delayed. The multi-threading example uses a pipe made from wrapping 2 UDP sockets connected back to back. You send() to one end and Mongoose waits on the other end. |
Beta Was this translation helpful? Give feedback.
From the docs
If you absolutely need multi-threading, take the time to go through the user guide (linked above) and tutorials, there is one that shows you how to do multi-threading.
Please read the official docs instead of comments/issues from 2013, 10 years ago. Mongoose is 7.8 now
Your OS provides a socket environment and a select()/poll() call. Mongoose will wait on those sockets, so reads are not delayed.
Writes are immediately sent to the socket if UDP, and to a buffer if TCP. The poll timeout is the time mg_mgr_poll() provide…