WaitForMultipleObjects , threads and blocking function
- From: mosfet <john.doe@xxxxxxxxxxxxx>
- Date: Wed, 02 Apr 2008 14:32:02 +0200
Hi,
I would like some help about a problem about Threading and blocking function.
We have developped a c++ wrapper around wininet to post some specific data.
Here is the architecture :
A thread always running and waiting on two events, when the event m_hStartRequest is signaled the HTTP request is written to server and once it has finished it send an event to inform the caller that the request is done.
DWORD WINAPI CHttpAdapter::ThrRequestAction( IN LPVOID lpvThreadParam )
{
HANDLE WaitHnd[2] = {0};
CHttpAdapter* pThis = (CHttpAdapter*) lpvThreadParam;
if ( pThis == NULL ) {
return -1;
}
WaitHnd[0] = pThis->m_hStartRequest;
WaitHnd[1] = pThis->m_hExitThread;
//
while (1)
{
DWORD dwRet = WaitForMultipleObjects( 2, WaitHnd, FALSE, INFINITE);
if ((dwRet == WAIT_OBJECT_0)) {
// Start our http request
BOOL bSend = FALSE;
do {
NetStream& stream = pThis->m_pRequest->GetRequestStream();
DWORD dwWrite = stream.Write( pThis->m_pBufferIn, pThis->m_ulSizeIn );
ATLTRACE(_T("CHttpAdapter::ThrRequestAction: Sent %d bytes.\n"), dwWrite);
bSend = stream.Close();
while( !bSend );
}
pThis->m_pWebResp = (HttpWebResponse*) pThis->m_pRequest->GetResponse();
...
::SetEvent( pThis->m_hRequestDone );
}
}
Ret_t CHttpAdapter::SendDataAndParseResponse()
{
Ret_t nRet = TP_ERR_OK;
EHttpErrorKind eHttpErrKind;
ATLTRACE(_T("CHttpAdapter::SendDataAndParseResponse()\n"));
// Inform thread to send the request
::SetEvent( m_hStartRequest );
DWORD dwRetType=::WaitForSingleObject(m_hRequestDone, 60000 );
if (dwRetType == WAIT_OBJECT_0)
eHttpErrKind = eHttpErr;
else if (dwRetType == WAIT_TIMEOUT)
eHttpErrKind = eTimeoutErr;
nRet = CheckResponse( eHttpErrKind );
return nRet
}
The problem arises when the thread is trying to write to server and server do not respond.
In this case the stream.Write stay blocked and after 1 min the caller(SendDataAndParseResponse) falls in timeout and in this case
I am trying to release it by calling the HttpEndRequest but it doen't work ...
.
- Follow-Ups:
- Re: WaitForMultipleObjects , threads and blocking function
- From: Joseph M . Newcomer
- Re: WaitForMultipleObjects , threads and blocking function
- Prev by Date: Re: CListBox sorting
- Next by Date: Re: How to display png file in toolbar
- Previous by thread: About WM_KEYDOW
- Next by thread: Re: WaitForMultipleObjects , threads and blocking function
- Index(es):