in reply to Handling abnormal thread termination
Threads usually “abnormally terminate” when they encounter an un-handled exception. Therefore, within the main body of your thread, wrap the actual work-processing part in an exception handling block. If the request throws an exception, mark the request as failed, record any error-information obtained, and send it back on the completed-requests queue, which will now receive both successful and failed requests.
Pseudocode for each request-processor thread or process:
while (not terminated) { wait for and receive request from incoming_request queue; try { perform_request; } except { request.failed = True; request.error_message = whatever; } post completed (or failed) request to finished_requests queue; }
If it is necessary to impose a time-limit per request, this same thread would set a timeout-interrupt to occur, which, if it does, would cause an exception to be raised. Timeout would thus result in a failed-request within the logic shown above.
To prevent unwanted accumulation of memory, many request processors are designed to execute a certain number of requests and then die. The parent process must therefore be waiting for children to die and must reap and then re-spawn those children.
Finally – you don’t have to write all this stuff from scratch. This is a very common requirement. So, CPAN has many existing examples.