[Libwebsockets] Thread safety with multiple contexts?

Andy Green andy at warmcat.com
Tue Sep 9 00:37:08 CEST 2014

On 8 September 2014 23:31:21 GMT+08:00, Mike M <lws14 at mordent.com> wrote:
>I have two lws contexts.  Is it safe to have one thread for each
>calling libwebsocket_service?  I.e. is lws thread safe by context?

Yes, there are no globals in the library.  So the library only touches stuff ultimately dereferenced by the context.  Stuff in a different context should be its own sealed universe.

There are some gotchas, you cannot share struct protocols in both contexts, because they get marked with a pointer to the context at context creation time.  However both contexts can have their protocols otherwise identical, reuse the same user callbacks etc, so you can copy one struct protocols each time.

You must service the contexts independently.

If it's not a separate process, both threads will share ulimits like number of file descriptors possible to open.

Otherwise that should be perfectly reliable.


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

More information about the Libwebsockets mailing list