Hello everyone.
I've tried to use an external select, the suspend/resume feature and a detached thread for some specific (slow) requests. The main idea around this, is: the common requests (database CRUDs, html/js/css sending etc.) are processed in the main loop within main thread (application), but, slow requests (large report generation, remote backups etc.) are suspended to be processed in a detached thread, which resumes the connection as soon as the request processing ends. However, I have had some difficulty to solve it because the MHD_queue_response() must be called by the main thread, not by detached thread(s).
After some googling I found two good related topics:
The 1. is the same problem I'm trying to solve, but I wouldn't like to use global variables. The 2. is exactly I'm looking for, but I couldn't find any code/example showing how the problem was solved, so I suspect he used the *con_cls instead of global variables to share some list reference to the detached thread(s).
After reading these topics I got two doubts. It is a good practice to use a shared vector (or any list + binary search) plus a mutex to manage each detached thread? If so, could the same list be used to be iterated to call the functions MHD_create_response_*() + MHD_queue_response() + MHD_resume_connection() to dispatch their respective responses? I need to take some care not to lose performance by choosing a bad design and, since some members tried to solve the same problem, I decided to ask.
I have a draft (in attachment) which I'll use to start/try the design using an external select + suspend/resume + pthread. It works fine if you uncomment the macro INTERNAL_SELECT to use the internal MHD loop or the macro TIMEOUT to use the external one. In this draft, the MHD_queue_response() is called in the detached thread, it is wrong, but it is just to understand how MHD works using external threads + suspend/resume. If you keep both INTERNAL_SELECT and TIMEOUT commented, the first request will never end unless another request arrives, I think a good design should solve it.
I would appreciate any good idea about this. If solved, it would be nice to convert it to a MHD example to share the solution for other members.
Thank you!
P.S.1: to test the draft above, use: