[Libwebsockets] lws_threadpool

Andy Green andy at warmcat.com
Tue Aug 28 14:10:06 CEST 2018

Hi -

I pushed on master an interesting (well, it's interesting to me...) 
addition to lws called "threadpool".

Basically it allows you to create one or more pool of threads with a 
FIFO queue for tasks to run on them, and provides synchronized 
integration for the threads with the lws service threads.  Tasks start 
out with an association to a wsi, but given there's not necessarily any 
relationship between the wsi lifecycle and whatever the task wants to 
do, that can detach cleanly according to what happens with the wsi.

The tasks can synchronize with the service thread by triggering a 
WRITABLE callback in the service thread context; when the connection is 
writable the callback handler can check the task status and write 
output, resume the task etc.

In other words, it leaves lws singlethreaded / nonblocking style but 
allows you to migrate longrunning processes to the threadpool that 
produce output and make it available in the WRITABLE callback without 
delaying the service thread at all.

It's implemented in pthreads, but actually lws hides all pthread apis, 
mutexes etc.  The actual user task function is very simple and the 
synchronization to the lws service thread is also hidden.

Docs are here




Minimal example


Although the example is for ws actually it works the same for http, raw 
etc, since they all use some variety of WRITABLE callback set by the role.

Since this is pretty new, any observations about how with a little more 
it could be useful for whatever are welcome (patches even more welcome)...


More information about the Libwebsockets mailing list