[Libwebsockets] client threading question

Andy Green andy at warmcat.com
Thu May 23 17:04:34 CEST 2019

On May 23, 2019 7:27:44 AM PDT, Dave Horton <daveh at beachdognet.com> wrote:
>I have an lws ws_client application that connects to multiple web
>socket servers and streams lots of data to them.  The client process
>itself is multithreaded, but I have one thread running the
>lws_service() call and one lws_context*.


>At some point (~500 connections on my current hardware) the lws_service
>thread consumes all of one CPU and the program starts to falter,
>although I have idle CPU on other cores.


>Is there any reason that I could not start multiple  threads (I’m
>thinking ~2-4) to run lws_service(), each with a corresponding
>lws_context* ?  

In the old days openSSL coild not handle being initialized more than once in the same process, but recent OpenSSL has referenced-counted apis for that which lws will use automatically if it finds they exist (when it looks at cmake-time).

By default each context creates a lookup table sized to ulimit -n, the max number of fds per process... if you're only planning 4 contexts, and say the process ulimit -n was set to 4096, you'd be burning 4 x 4096 x sizeof void *,  or 128KB for 64-bit... it's probably not killing you.

And from to sound of it, thw lws service thread must already take care of locking so that won't especially burden you.
  But you may need to revisit your locking from the point of view the lws service threads may truly be concurrently touching things and competing between themselves, not just one of those in competition with your other threads.

Lws does support multiple service threads, but I have only intensively tested it for server... there's a http-server-smp example to show how that works.  But you can probably ignore it and roll with n contexts in your case.


>Libwebsockets mailing list
>Libwebsockets at ml.libwebsockets.org

More information about the Libwebsockets mailing list