[Libwebsockets] Multithreaded server / performance with high libwebsocket_service() timeout

Thomas Körper Thomas.Koerper at esd.eu
Wed Apr 3 08:32:58 CEST 2013


Hello,

I've a main server thread basically just doing this:

    while (1) {
        libwebsocket_service(ws_context, LONG_TIME);
    }

Now another thread gets an external event and needs to send something...

Intuitively I would let it call libwebsocket_callback_on_writable(), what would interrupt the libwebsocket_service() call and give the main thread a writeable callback.

But that's neither right nor allowed, as I might call lws for the same context concurrently from different threads.
    
So this lead me to something like this for the main thread:
 
    while (1) {
        LOCK();
        libwebsocket_service(ws_context, LONG_TIME);
        UNLOCK();
    }

When the other thread now wants to send something it does this:

    LOCK();
    libwebsocket_callback_on_writable(...);
    UNLOCK();

That's perfectly safe, I assume.

But... In the worst case it takes LONG_TIME until I get my writeable callback, which leads to a very poor performance of my application.

Where's my mistake? (I don't want to use zero timeout/waste CPU, of course)


Regards,
    Thomas





More information about the Libwebsockets mailing list