OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 6:03 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Win32 Threads
PostPosted: Fri Dec 20, 2002 9:23 am 
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.


Top
  
 
 Post subject: Re:Win32 Threads
PostPosted: Fri Dec 20, 2002 7:39 pm 
Quote:
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.
Quote:
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.


Top
  
 
 Post subject: Re:Win32 Threads
PostPosted: Fri Dec 20, 2002 8:10 pm 
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:
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:
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.


Top
  
 
 Post subject: Re:Win32 Threads
PostPosted: Sat Dec 21, 2002 2:56 am 
Guest wrote:
Does CloseHandle() have to be called on threads created with _beginthread()?

Like I said, _beginthread closes the handle for you, so no.
Quote:
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.


Top
  
 
 Post subject: Re:Win32 Threads
PostPosted: Sat Dec 21, 2002 3:56 am 
Thanks for your help.

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


Top
  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 29 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group