[Libwebsockets] Interaction of external threads with libwebsockets server

Thomas Spitz thomas.spitz at hestia-france.com
Tue Dec 17 09:03:43 CET 2013


Hello everyone,

I am trying to find a way for external threads to ask libwebsocket server
to send websocket message on their behalf (as they cannot do it directly).
The only way I see at the present is to make a loop with
libwebsocket_service (context, VERYSHORTTIME); and test potential incoming
messages from other threads. This is not very elegant as CPU computes all
the time and not very reliable as I should put VERYSHORTTIME at 0 if I
don't want to miss any message.

I tried the following modification in test-server.c :

void *connexionServeur(void *arg) {
> struct libwebsocket_context *context = arg;
> while (1) {
> if(libwebsocket_service(context, 60000)<0){
>  printf("Problem!\n");
> break;
> }
> }
>  return NULL ;
> }
> int statut_Thread;
> pthread_t Thread;
> statut_Thread = pthread_create(&Thread, NULL, connexionServeur, context );
>  if (statut_Thread != 0) {
> printf("Problem with thread creation \n");
> }
> while (n >= 0 && !force_exit) {
> struct timeval tv;
> gettimeofday(&tv, NULL);
> if (((unsigned int)tv.tv_usec - oldus) > 50000) {
>
> libwebsocket_callback_on_writable_all_protocol(&protocols[PROTOCOL_DUMB_INCREMENT]);
> oldus = tv.tv_usec;
> libwebsocket_service(context, 0);
>  }
> usleep(10000);
> }


but it crashes quickly as soon as I try drawing . I suppose that
libwebsocket_service is called twice at the same time and it creates
conflicts that lead to stop crash the websocket.

I have seen in the mailing list that Thomas Koeper has already had a
similar question
http://ml.libwebsockets.org/pipermail/libwebsockets/2013-April/000403.html .
At that time, he was thinking of creating a mutex on libwebsocket_service.
Do you think this is the right solution?

Thanks in advance for your help
Best regards,
Thomas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://libwebsockets.org/pipermail/libwebsockets/attachments/20131217/ee01ccb3/attachment.html>


More information about the Libwebsockets mailing list