Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multithreading and ADO

Status
Not open for further replies.

Chandhru

Technical User
Jun 12, 2003
77
US
Hi,

I am creating multiple threads to access a database and retrieve records using ADO. I am getting runtime error "abnormal program termination'.

My main() function creates an Database connection and recordset object. it also creates multiple thread which accesses ThreadFunc() . it passes the recordset object to ThreadFunc().

ThreadFunc() function receives the recordset object prints the record and moves the pointer to the next record.

I am attaching the code below. please correct me.

Code:
Hi,

   I am creating multiple threads to access a database and retrieve records using ADO. I am getting runtime error "abnormal program termination'. 

    My main() function creates an Database connection and recordset object. it also creates multiple thread which accesses ThreadFunc() . it passes the recordset object to ThreadFunc().

    ThreadFunc() function receives the recordset object prints the  record and moves the pointer to the next record.

   I am attaching the code below. please correct me.

[code]
#import "C:\Program files\Common Files\System\Ado\msado15.dll" no_namespace rename("EOF", "ADOEOF")

#include <windows.h>
#include <stdio.h>
#include <ole2.h>
#include <conio.h>

#define NUM_THREADS 12

CRITICAL_SECTION cs; 
HRESULT hr;
_ConnectionPtr pConn;
_RecordsetPtr pRs;

/*
    ThreadFunc is responsible for accessing the Recordset. and moving through
	recordset.
*/
DWORD WINAPI ThreadFunc(LPVOID lpParam) 
{
      EnterCriticalSection(&cs);
	  _bstr_t val = pRs->Fields->Item[_variant_t("BTN")]->Value;
	  printf("%s\n",(char*)val);    
	  pRs->MoveNext();
      //printf("Hello World, I'm thread# %d\n", (int)lpParam);
	  //printf("hello world \n");
      LeaveCriticalSection(&cs);
      return 0;
}//ThreadFunc

void main(){

	DWORD dwThreadId;
    HANDLE hThread[NUM_THREADS];
    int n;    
	CoInitialize(NULL);
	InitializeCriticalSection(&cs);
    // Create an ADO connection to database
	try{
		hr = pConn.CreateInstance(__uuidof(Connection));
		hr = pRs.CreateInstance(__uuidof(Recordset)); 
		_bstr_t strConn("Provider=sqloledb;server=SINF005;Trusted_Connection=yes;database=Core;");
		pConn->Open(strConn,"","",adConnectUnspecified);
		pRs->Open("SELECT top 10 Npa_Num from Npa (NOLOCK)", pConn.GetInterfacePtr(), adOpenForwardOnly, adLockReadOnly, adCmdText);
		//pRs->Open("SELECT BTN from mbs_GetSpitFireEasyPay (NOLOCK)", pConn.GetInterfacePtr(), adOpenForwardOnly, adLockReadOnly, adCmdText);
	}catch(_com_error &e){
		printf("Error\n");
		printf("\tCode meaning = %s", e.ErrorMessage());
	} 

	// create all the threads
    for (n = 0; n < NUM_THREADS; n++)
    {
        hThread[n] = CreateThread(NULL, 0, ThreadFunc, &pRs, 0, &dwThreadId);  
        if (hThread == NULL)
            fprintf(stderr, "Failed to create thread# %d", n);
    }//for
	for (n = 0; n < NUM_THREADS; n++)
    {
        if (hThread[n] != NULL)
            WaitForSingleObject(hThread[n], INFINITE);
    }//for

    DeleteCriticalSection(&cs);
	printf("This is after all threads exited\n");
	CoUninitialize();
}


Thanks
 
Try to step with debugger and say what is exactly the line where you program crashes.

Ion Filipski
1c.bmp
 
I tried the debugger, i found an yellow mark in my main function. It gave Unhandled Exception in Multi-ADO.exe (kernel32.DLL):0xE06D763:Micorosoft C++ Exception.

I am having a doubt whether am I passing the recordset object correctly to the ThreadFunc().

 
What exactly is the line that fail in your code? use StepOver(F10) to step each line.

Ion Filipski
1c.bmp
 
I am getting error in waitforsingleobject line. It shows unhandled exception.

 
Look at this:
Code:
for (n = 0; n < NUM_THREADS; n++)
{
   hThread[n] = CreateThread(NULL, 0, ThreadFunc, &pRs, 0, 
                                    &dwThreadId);  
   if (hThread == NULL)
      fprintf(stderr, "Failed to create thread# %d", n);
}
for (n = 0; n < NUM_THREADS; n++)
{
   if (hThread[n] != NULL)
         WaitForSingleObject(hThread[n], INFINITE);
}

you wait until the first thread is finished. After that you wait until the second thread is finished, but it could terminate before first thread is finished. If you start threads in some order that does not mean at all what they will terminate in the same order. Instead of the second loop try to use WaitForMultipleObjects without a loop.

Ion Filipski
1c.bmp
 
by the way, are you sure you call WaytForSingleObject before threads are terminated?

Ion Filipski
1c.bmp
 
IonFilipsky, you are right. I wanted all my threads to exit only after retrieving all the records from the database. how can we make sure that threads exit only after all the records in the database are retrieved and not before that. please help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top