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 ?
Asked
Active
Viewed 1,256 times
2 Answers
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
:
- While the worker thread is inside the while loop
while (consume_socket(ctx, &conn->client))
, you could consider the worker thread as busy. - 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
-
You should use an interlocked increment to avoid errors in that count. – jmucchiello Jun 10 '13 at 01:11