Click here to Skip to main content
15,912,932 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: setting the application icon at runtime Pin
Jim Crafton25-Aug-03 16:38
Jim Crafton25-Aug-03 16:38 
GeneralRe: setting the application icon at runtime Pin
Toni7825-Aug-03 20:36
Toni7825-Aug-03 20:36 
GeneralRe: setting the application icon at runtime Pin
DaFrawg29-Sep-03 2:27
DaFrawg29-Sep-03 2:27 
QuestionHow to include dialog in the view Pin
Binayak25-Aug-03 13:20
Binayak25-Aug-03 13:20 
GeneralControl Bars Pin
Xakep25-Aug-03 12:59
Xakep25-Aug-03 12:59 
GeneralRe: Control Bars Pin
Thomas Lau25-Aug-03 16:22
Thomas Lau25-Aug-03 16:22 
GeneralRe: Control Bars Pin
Mike Dimmick25-Aug-03 22:42
Mike Dimmick25-Aug-03 22:42 
GeneralClosing the handle of a dynamically spawned thread Pin
Kuniva25-Aug-03 12:17
Kuniva25-Aug-03 12:17 
I have a TCP server which starts a thread that loops to accept connections. The funtion the "Acceptor" is spawned from then uses WaitForSingleObject() to wait for the acceptor to return for any reason (there is actually first a suicideEvent which is triggered when user pressed CTRL-c and that even is also waited for with WaitForSingleObject(), then a flag is set to let the acceptor know that it needs to exit). This way, it works fine cause i can close the handle to the acceptor thread once WaitForSingleObject returned. But then...

The Acceptor spins every new connection off into its own clientthread. I keep a list of thread handles in a vector. So when a new thread is created, i add the handle to the vector. Now the problem is... I read on MSDN that the right way to actually terminate a thread is to let it return or call ExitThread and then to use CloseHandle() to remove it from memory.. cause if not all handles are closed, it won't be removed from memory, and i think that would be a gigantic problem for a tcp server since that way, everytime a connection is lost, the memory the thread took up is wasted and stays allocated. Cause my problem is i don't know how/when to use CloseHandle now... When a client thread terminates, i have no way to see it did this... I thought about using WaitForSingleObject or WaitForMultipleObjects again but.. i don't see how its possible to implement this because the acceptor can't be waiting to see which threads have finished...
I'll provide a basic sketch of my code (very much simplified):

void StartServer()
{
    // run the acceptor thread and use WaitForSingleObject to wait for a suicide event
    // then set the flag for the acceptor to exit and wait for the acceptor to exit using WaitForSingleObject, then close the handle to the acceptor so its removed from memory
}

DWORD WINAPI Acceptor(void*)
{
    // loop for connections and spin them off in their own clientthread, at the same time check if the flag is set to make us exit (using select() we can check for incoming connection non-blocking)
    // If a connection is accepted store the handle of the client thread in a global vector
}

DWORD WINAPI Clientthread(void*)
{
    // Handle IO here and return when peer closes the connection or if any other error occurs
    // Cannot close handle to thread here, cause we're still in it
    // If we just return, the thread stays mapped in memory, so everytime someone disconnects memory is wasted, causing the server to run out of memory eventually
}


I hope i made the problem clear... i really have tried to think of several solutions but they all did not turn out well in the end. If anyone has any suggestions, please let me know.

Many thanks

Kuniva
--------------------------------------------
GeneralRe: Closing the handle of a dynamically spawned thread Pin
Mike Dimmick25-Aug-03 22:43
Mike Dimmick25-Aug-03 22:43 
GeneralDialog prompts in German - would like to restore to English Pin
Peter Maton25-Aug-03 11:15
Peter Maton25-Aug-03 11:15 
GeneralRe: Dialog prompts in German - would like to restore to English Pin
Peter Maton26-Aug-03 13:08
Peter Maton26-Aug-03 13:08 
Generalcolor table of bitmap Pin
includeh1025-Aug-03 10:34
includeh1025-Aug-03 10:34 
GeneralRe: color table of bitmap Pin
Joaquín M López Muñoz25-Aug-03 10:42
Joaquín M López Muñoz25-Aug-03 10:42 
GeneralSimple question. Pin
Jon Newman25-Aug-03 10:03
Jon Newman25-Aug-03 10:03 
GeneralRe: Simple question. Pin
Joaquín M López Muñoz25-Aug-03 10:39
Joaquín M López Muñoz25-Aug-03 10:39 
GeneralRe: Simple question. Pin
Jon Newman25-Aug-03 10:42
Jon Newman25-Aug-03 10:42 
GeneralTemplate for experts!! help Pin
berserker_r25-Aug-03 9:40
berserker_r25-Aug-03 9:40 
GeneralRe: Template for experts!! help Pin
Anonymous25-Aug-03 10:19
Anonymous25-Aug-03 10:19 
GeneralRe: Template for experts!! help Pin
Joaquín M López Muñoz25-Aug-03 10:27
Joaquín M López Muñoz25-Aug-03 10:27 
Questionhow to show data in internet explorer Pin
Binayak25-Aug-03 8:20
Binayak25-Aug-03 8:20 
AnswerRe: how to show data in internet explorer Pin
PremL26-Aug-03 5:39
PremL26-Aug-03 5:39 
Generalusing CArchive with CBlockingSocket class Pin
Mike Ellertson25-Aug-03 7:56
sussMike Ellertson25-Aug-03 7:56 
GeneralRe: using CArchive with CBlockingSocket class Pin
Mikey_E25-Aug-03 8:30
professionalMikey_E25-Aug-03 8:30 
GeneralProblem with MFC create control for component Pin
tongc25-Aug-03 7:13
tongc25-Aug-03 7:13 
GeneralSHGetFileInfo Pin
Member 50856725-Aug-03 7:02
Member 50856725-Aug-03 7:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.