HTTPServer with custom thread pool (to have custom, expensive thread data) #4069
Replies: 1 comment 1 reply
-
I have made some progress. The magical template
I believe this applies to any Poco thread created. Fortunately it seems like (in my program at least) the only threads created are those to handle web requests. So this bypasses my need to use a custom thread pool generating custom threads. The thing I don't seem to have control over is when these threads get created and destroyed. What I've found is that, for the first several web requests, threads are created and destroyed a lot, but then it settles down, to the point where, after a while, no matter what web requests I throw at the HTTP server (at least in the way my program throws web requests at it), it doesn't create or destroy threads any more, but sticks with the existing set of 7 threads. I number the threads as they are created. The set it settled on was [ 9, 11, 12, 13, 15, 16, 17 ] -- so it looks like only 17 threads are created in total, and 10 of those threads are destroyed along the way. Those 10 wasted threads are not ideal for my situation, but the fact that it settles down to this fixed set seems promising. |
Beta Was this translation helpful? Give feedback.
-
Short version: I don't understand how to get HTTPServer to use a threadpool that is customized so that each thread can store/use custom data. Also, I ideally want the set of threads to be fixed throughout the application lifetime.
Long version:
I'm want my Poco::Net::HTTPServer to use threads drawn from a pool of custom threads/runnables. I am trying to give each thread its own OpenGL window and context. So I want there to be, say, 16 threads, each precreated and running/waiting, each having set up its own OpenGL stuff (OpenGL per se is not important to this question, it could be any thread-specific data that is expensive to set up and tear down). If at any time the HTTPServer needs more threads, I guess that's okay, and it could make a new one. But it would actually be ideal if the HTTPServer would just block on new requests until one of the original 16 threads is available. I'm not sure what HTTPServer does when it receives a no-more-threads-available error from the thread pool.
Along the same lines, I don't want the HTTPServer or the thread pool to kill idle threads, again because their thread-specific data is expensive to set up and tear down.
I see the pieces but I'm having trouble putting them together.
I see that Poco::Net::HTTPServer() can take a thread pool parameter. And I see how to set up a runnable. I see how to start that runnable on a thread. But I don't know how to make a custom thread pool made of threads that are associated with my runnable.
And then, as I say above, ideally I would like it if the threads in the thread pool are just created once at program start, and terminated at program end, and for the HTTPServer to block requests until there is a thread available to handle them, instead of making new threads.
Beta Was this translation helpful? Give feedback.
All reactions