[Libwebsockets] lws_callback_on_writable() calling thread?

Andy Green andy at warmcat.com
Tue Jan 28 12:47:26 CET 2020

On 1/28/20 11:40 AM, Zevv wrote:
> Quoting Sumit Dubey (2020-01-28 12:24:02)
>> Is it possible to call lws_callback_on writable() from a thread other than the one calling lws_service()? I can imagine a race condition between the thread that closes the wsi and the one that calls lws_callback_on writable().
>> Thank you.Sumit Dubey
> Afaik this is a no-no: lws is strictly single threaded, see
> the docs in
>    https://libwebsockets.org/lws-api-doc-master/html/md_README.coding.html

Right... there's one exception though, lws_cancel_service() api is 

It writes a byte into a pipe that's created for the purpose, so other 
threads can create an event on the event loop to wake it and make it 
aware another thread has work for it.

(On freertos platform, it has no pipe(), but it creates a UDP socket 
pair with one bound to for the same purpose).

On waking, which typically happens immediately, the event loop 
understands it was woken by that and broadcasts an 
LWS_CALLBACK_EVENT_WAIT_CANCELLED callback from the event loop thread 
context, so anything interested can lock shared objects and check what 
it's supposed to be doing from the lws thread context.


More information about the Libwebsockets mailing list