tmolnar
asked on
Problems with threads.
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_hEv entKill, 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_hEve ntKill, INFINITE);
}
}
}
};
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_hEv
{
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_hEve
}
}
}
};
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
If I do that, I have to cut out the will the
while (WaitForSingleObject(m_hEv entKill, 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())?
while (WaitForSingleObject(m_hEv
{
...
}
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())?
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.
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.
ASKER
Thax!! It's OK now...
ASKER
Thanx!! It's OK now...
ASKER