0

I am starting mongoose web server with x threads. Is there a way that I can log when all x threads are busy so I can increase the thread count if required ?

JohnnyHK
  • 305,182
  • 66
  • 621
  • 471
Vivek Goel
  • 22,942
  • 29
  • 114
  • 186

2 Answers2

2

That is not possible without changing the code of Mongoose. I would, for example, change the static void worker_thread(struct mg_context *ctx) function in mongoose.c:

  1. While the worker thread is inside the while loop while (consume_socket(ctx, &conn->client)), you could consider the worker thread as busy.
  2. After close_connection(conn); the worker thread is free for processing a new event in the socket queue.

You can use that point for counting the number of busy threads.

diewie
  • 754
  • 4
  • 8
1

As diewie suggested, you can:

  • add "int num_idle" to the struct mg_context
  • in consume_socket, do:

    ctx->num_idle++;
    
    // If the queue is empty, wait. We're idle at this point.
    while (ctx->sq_head == ctx->sq_tail && ctx->stop_flag == 0) {
      pthread_cond_wait(&ctx->sq_full, &ctx->mutex);
    }
    
    ctx->num_idle--;
    assert(ctx->num_idle >= 0);
    if (ctx->num_idle == 0) {
      ... your code ...
    }
    
valenok
  • 827
  • 7
  • 9