Win32 Threads

Programming, for all ages and all languages.
Post Reply
Guest

Win32 Threads

Post by Guest »

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.
Tim

Re:Win32 Threads

Post by Tim »

My questions is how and when do I close the handle returned from this function?
_beginthread closes the handle for you before it returns. _beginthreadex doesn't, and returns a handle to the thread.
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?
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.

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.
Guest

Re:Win32 Threads

Post by Guest »

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

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;
      }
   }
}
Is this function above a waste of CPU time, because I have seen some examples that only do this

Code: Select all

int Read( void * pData, unsigned int iLen)
{
   if(!pData || !iLen)
      return(-1);

   return(recv(m_Socket, (char*)pData, iLen, 0));
}
Obviously if this second function will get as many bytes as iLen in one go, it is far better.

Can someone please clarify?

thanks.
Tim

Re:Win32 Threads

Post by Tim »

Guest wrote:Does CloseHandle() have to be called on threads created with _beginthread()?
Like I said, _beginthread closes the handle for you, so no.
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.
Not necessarily. The first way you showed is the way you should be doing it.
Guest

Re:Win32 Threads

Post by Guest »

Thanks for your help.

PS: sorry for asking about the handle twice, I never read your answer properly ::)
Post Reply