I’m writing a small network event server for my application; the project is based on threads.

Each connection to a client is served by a thread which the main task generates after Accept:

while (1) { TSocket *s = lsocket->Accept (); TThread *thread = new TThread ("es_handler", es_handler::run_thread, s); thread->Run (); }
Since the main task is not interested on the future of the childs I would like to detach the thread. Unfortunatelly the root interface does not export a method to detach, even if the fDetached variable exists.
A secon problem regards the TThred istance itself: the main task will not use the pointer to childs, then to avoid memory leaks I would delete them, but this is not valid (since childs are running). Any idea?
PS: the same leak problem came form the s pointer which cannot be deleted by the main task.

Dear Eto,

TThread detaches automatically threads if the function to be run returns void. If the fucntion return (void *) the thread is not detached.

For what relates to cleanup, you should either keep track of the objects which need to be destroyed and do it when the related thread is finished (testing TThread::GetState() == TThread::kFinishedState ) or do it inside the thread function just before exiting.

I have attached an example of a simple server as yours implementing the second solution. There is also a client macro that just receives a message once it is connected.

Hope it helps.

Gerri Ganis
Clnt.C (321 Bytes)
MTSrv.C (1.47 KB)