Hi,
I have just started properly learning threads for win32. I have found that if I want to use c runtime functions in my threads procedures, I must use _beginthread to prevent memory leaks.
My questions is how and when do I close the handle returned from this function?
When creating a thread using CreateThread() I must call CloseHandle() on my thread handle, I think this is to be done as soon as I don't need the handle anymore, even if the thread has not finished?? Is this right?
thanks all.
Win32 Threads
Re:Win32 Threads
_beginthread closes the handle for you before it returns. _beginthreadex doesn't, and returns a handle to the thread.My questions is how and when do I close the handle returned from this function?
Right. CloseHandle just decrements the reference count of a handle; if the count reaches zero, it frees the handle. When a thread is created it has two references: one by the thread that created it, and one by the thread itself. You can imagine that, when the child thread exits, it does CloseHandle on itself.When creating a thread using CreateThread() I must call CloseHandle() on my thread handle, I think this is to be done as soon as I don't need the handle anymore, even if the thread has not finished?? Is this right?
Therefore calling CloseHandle in the parent won't kill the child; also, if you don't call CloseHandle in the parent, the child thread handle will not be freed.
Re:Win32 Threads
Does CloseHandle() have to be called on threads created with _beginthread()?
Also, I have a question about sockets, with a TCP/IP socket (stream socket), does a single call to recv() get all data waiting on the socket? I have been experimenting and so far I know that it will get 1KB in one big gulp. Does the TCP/IP stack retireve all of the buffer by default.
In case I am not making any sense I will give you a small example
Is this function above a waste of CPU time, because I have seen some examples that only do this
Obviously if this second function will get as many bytes as iLen in one go, it is far better.
Can someone please clarify?
thanks.
Also, I have a question about sockets, with a TCP/IP socket (stream socket), does a single call to recv() get all data waiting on the socket? I have been experimenting and so far I know that it will get 1KB in one big gulp. Does the TCP/IP stack retireve all of the buffer by default.
In case I am not making any sense I will give you a small example
Code: Select all
bool ReadAsciiSocket(SOCKET Sck, char *pData, int nMaxBytes, char cTerm = '\0') {
int nDataPtr = 0, nBufferLeft = nMaxBytes, nLastRead;
memset(pData, 0, nMaxBytes);
while (true) {
nLastRead = recv(Sck, &pData[nDataPtr], nBufferLeft, 0);
if (nLastRead > 0) {
nDataPtr += nLastRead;
nBufferLeft -= nLastRead;
if (pData[nDataPtr - 1] == cTerm) {
return true;
}
} else {
return false;
}
}
}
Code: Select all
int Read( void * pData, unsigned int iLen)
{
if(!pData || !iLen)
return(-1);
return(recv(m_Socket, (char*)pData, iLen, 0));
}
Can someone please clarify?
thanks.
Re:Win32 Threads
Like I said, _beginthread closes the handle for you, so no.Guest wrote:Does CloseHandle() have to be called on threads created with _beginthread()?
Not necessarily. The first way you showed is the way you should be doing it.Also, I have a question about sockets, with a TCP/IP socket (stream socket), does a single call to recv() get all data waiting on the socket? I have been experimenting and so far I know that it will get 1KB in one big gulp. Does the TCP/IP stack retireve all of the buffer by default.
Re:Win32 Threads
Thanks for your help.
PS: sorry for asking about the handle twice, I never read your answer properly ::)
PS: sorry for asking about the handle twice, I never read your answer properly ::)