Solved

Problems with threads.

Posted on 1999-01-21
6
458 Views
Last Modified: 2013-11-20
Hello!
I have a multithreaded Client/Server. When clients are beginning to connect to this, the of all OS begins to work slow (immediately when I start new threads, the system works visible slower - after only two threads!!). The priority of threads is normal. The C-S communication is done through TCP/IP, using handles to CAsyncSocket derived classes, using WinSock API.
What is the problem??
Here is the looping code of the threads, which communicate with clients:

while (WaitForSingleObject(m_hEventKill, 0) == WAIT_TIMEOUT)
      {
            i =  recv( m_hSocket, b, 2, 0);
            if (i == 2)
            {
                    Do something.....
            }
            if (i == SOCKET_ERROR )
            {
                  switch (WSAGetLastError())
                  {
                  case WSAETIMEDOUT :
                  case WSAECONNRESET :
                  case WSAECONNABORTED :
                        {
                              m_theMessage = "@@##$$conerr";
                              ProcessPendingRead();
                              WaitForSingleObject(m_hEventKill, INFINITE);
                        }
                  }
            }
      };
0
Comment
Question by:tmolnar
  • 4
  • 2
6 Comments
 

Author Comment

by:tmolnar
ID: 1328104
Edited text of question
0
 
LVL 4

Accepted Solution

by:
piano_boxer earned 50 total points
ID: 1328105
You are doing a BUSY wait because the timeout value for WaitForSingleObject is zero.

You can switch to using CAsyncSocket class the handles incomming data async. Derive your own class from CAsyncSocket and override OnReceive() and OnClose() functions.

OnReceive() will be called when there are some data ready to be read, and OnClose() will be called when the socket is closed.

You can add the derived CAsyncSocket class as a variable in you CWinApp and initialize it in OnInitInstance():

OnInitInstance(...)
{
   [...]
   m_MySocket.Create(12345, SOCK_STREAM, FD_READ|FD_CLOSE);
   [...]
}


Another solution is to use Winsock2 and use the WSAEventSelect function to enable signalling of a Win32 event when something happens on the socket.
0
 

Author Comment

by:tmolnar
ID: 1328106
If I do that, I have to cut out the will the
while (WaitForSingleObject(m_hEventKill, 0) == WAIT_TIMEOUT)
{
  ...
}
to avoid the busy wait...
But how to know, that the main thread want to kill the thread?
(see the m_hEventKill event)
Anoher question: the OnReceive() function will be called by the system, and will be work in the main thread on in the "client" thread?
Another problem is, that when I try to read in nonblocking mode all the time, I got an error, when the connection failed. How to get a corresponding error notification, if I use a CAsyncSocket derived class (something like On Error())?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Expert Comment

by:piano_boxer
ID: 1328107
I just re-read the question and there is really no need for you to use the thread function if you use the CAsyncSocket class.

Just drop the thread-thing for now.

The CAsyncSocket::OnReceive() function is call in the context of the thread that created the socket and is called by MFC through an internal hidden window MFC creates in CAsyncSocket::Create(). Windows posts socket event messages to this hidden windows and MFC calls the OnXxxx messages in response to them.

To read data from the socket do something like this in your OnReceive() overriden function:

void CMySocket::OnReceive(int nErrorCode)
{
    BYTE buf[512];
    CString strFromAddr;
    UINT nFromPort;

    int len = ReceiveFrom(buf, sizeof(buf), strFromAddr, nFromPort);
    if(len==0)
        // Connection closed !!!
    if(len>0)
    {
        // Use data in the buffer (buf).
    }
}

Here you also get the IP address and port of the sender. If you dont want this, just use the Receive() call instead.

Look at the nErrorCode passed to OnReceive() for errors.
0
 

Author Comment

by:tmolnar
ID: 1328108
Thax!! It's OK now...
0
 

Author Comment

by:tmolnar
ID: 1328109
Thanx!! It's OK now...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now