[Libwebsockets] One thread for initializing and reading, another for writing

Andrew Cooks acooks at linux.com
Thu Oct 1 09:37:11 CEST 2015


Hi Aubin

On Wed, Sep 30, 2015 at 8:28 PM, MAHE Aubin <andy.green at linaro.org> wrote:

> Hi All,
>
> I use a thread for reading on libwebsockets and another one for writing.
>
> More precisely, the first thread is dedicated to:
>         libwebsocket_create_context()
>         libwebsocket_service()
>         libwebsocket_callback_on_writable_all_protocol()
>         http and user callbacks defined in struct libwebsocket_protocols
>
> The second thread uses only libwebsocket_write().
>
> Is this practice safe?
>

In README.coding.md there is a section that says that you should only
'send' when you receive the 'LWS_CALLBACK_SERVER_WRITEABLE' or
'LWS_CALLBACK_CLIENT_WRITEABLE',
which I interpret to mean that it is not safe to call libwebsocket_write()
from another thread.

I'm new to libwebsockets too (so my solution might not be optimal), but for
my application I implemented a message queue that I could write to from
another thread and read from inside the 'LWS_CALLBACK_CLIENT_WRITEABLE'
callback. It took a bit of work to get the publish/subscribe mechanics to
work properly to support multiple clients.

I'd like to learn how others have approached this problem too.

Regards,

a.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://libwebsockets.org/pipermail/libwebsockets/attachments/20151001/9e0f4001/attachment.html>


More information about the Libwebsockets mailing list