You are still calling CoUninitialize() in the middle of your main thread.
Delete it from here:
Code:
}
CoUninitialize();
IGlobalInterfaceTable *lpGIT;
and put it here:
Code:
printf("This is after all threads exited\n");
CoUninitialize();
return 0;
>> 2. The second problem is the main thread doesnt exits (it hangs on). <<
Sorry, there was a mistake in the code I posted. You also have an extra semi-colon that shouldn't be there.
Code:
while (MsgWaitForMultipleObjects(1, &hThread[n], TRUE,INFINITE, QS_ALLEVENTS) == WAIT_OBJECT_0 + 1);{
should be:
Code:
// Pass FALSE instead of TRUE and remove extra semi-colon.
while (MsgWaitForMultipleObjects(1, &hThread[n], FALSE,INFINITE, QS_ALLEVENTS) == WAIT_OBJECT_0 + 1) {
Also, we are not releasing the critical section if ADOEOF.
Change:
Code:
EnterCriticalSection(&cs);
if (pRs->ADOEOF) break;
_bstr_t val = pRs->Fields->GetItem(_variant_t("Npa_num"))->Value;
//_bstr_t val = pRs->Fields->Item[_variant_t("BTN")]->Value;
pRs->MoveNext();
LeaveCriticalSection(&cs);
to:
Code:
EnterCriticalSection(&cs);
if (pRs->ADOEOF)
{
LeaveCriticalSection(&cs);
break;
}
_bstr_t val = pRs->Fields->GetItem(_variant_t("Npa_num"))->Value;
//_bstr_t val = pRs->Fields->Item[_variant_t("BTN")]->Value;
pRs->MoveNext();
LeaveCriticalSection(&cs);
>> 1. sometimes I get only one record being printed twice (This happens first time when I build and atleast once in 10 attempts). <<
See if the above fixes remove the problem. If it still occurs is there any pattern? Is it always the first or last records, or is it an arbitary record that gets repeated?
EDIT:
Here is the code with the above corrections incorporated:
Code:
#import "C:\Program files\Common Files\System\Ado\msado15.dll" no_namespace rename("EOF", "ADOEOF")
#include <ole2.h>
#include <stdio.h>
#include <windows.h>
#define NUM_THREADS 48
CRITICAL_SECTION cs;
HRESULT hr;
DWORD dwRsCookie;
/*
This function is used to make call in the future. for testing Iam
giving some delay here
*/
int ProcessFunc(){
int a,b,j,k;
for(a=0;a<2000;a++){
j=j++;
k=k++;
}
for(b=0;b<20000000;b++){
j=j--;
k=k--;
}
return 0;
}
/*
ThreadFunc is responsible for accessing the Recordset. and moving through
recordset.
*/
DWORD WINAPI ThreadFunc(LPVOID lpParam){
try
{
int i = 0;
printf("Thread started \n");
CoInitialize(NULL);
_RecordsetPtr pRs;
IGlobalInterfaceTable *lpGIT;
CoCreateInstance(CLSID_StdGlobalInterfaceTable, NULL,CLSCTX_INPROC_SERVER,
IID_IGlobalInterfaceTable, (void **)&lpGIT);
lpGIT->GetInterfaceFromGlobal(dwRsCookie,__uuidof(_Recordset),(void**)&pRs);
while(TRUE){
EnterCriticalSection(&cs);
if (pRs->ADOEOF)
{
LeaveCriticalSection(&cs);
break;
}
_bstr_t val = pRs->Fields->GetItem(_variant_t("Npa_num"))->Value;
//_bstr_t val = pRs->Fields->Item[_variant_t("BTN")]->Value;
pRs->MoveNext();
LeaveCriticalSection(&cs);
ProcessFunc();
printf("%d %s \n",i++ ,(char*)val);
}
lpGIT->Release();
}catch(_com_error &e){
printf("\tCode = %08lx\n", e.Error());
printf("\tCode meaning = %s\n", e.ErrorMessage());
}
CoUninitialize();
return 0;
}
int main(){
DWORD dwThreadId;
HANDLE hThread[NUM_THREADS];
int n;
CoInitialize(NULL);
_ConnectionPtr pConn;
_RecordsetPtr pRs;
try{
hr = pConn.CreateInstance(__uuidof(Connection));
hr = pRs.CreateInstance(__uuidof(Recordset));
_bstr_t strConn("Provider=sqloledb;server=ALBHM01WSINF005;Trusted_C onnection=yes;database=Core;");
pConn->Open(strConn,"","",adConnectUnspecified);
pRs->Open("SELECT top 25 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());
}
IGlobalInterfaceTable *lpGIT;
CoCreateInstance(
CLSID_StdGlobalInterfaceTable,
NULL,CLSCTX_INPROC_SERVER,
IID_IGlobalInterfaceTable,
(void **)&lpGIT);
lpGIT->RegisterInterfaceInGlobal(pRs,__uuidof(_Recordset),&dwRsCookie);
InitializeCriticalSection(&cs);
// create all the threads
for (n = 0; n < NUM_THREADS; n++){
hThread[n] = CreateThread(NULL, 0, ThreadFunc, &pRs, 0, &dwThreadId);
if (hThread[n] == NULL)
fprintf(stderr, "Failed to create thread# %d", n);
}//for
for (n = 0; n < NUM_THREADS; n++){
if (hThread[n] != NULL){
while (MsgWaitForMultipleObjects(1, &hThread[n], FALSE,INFINITE, QS_ALLEVENTS) == WAIT_OBJECT_0 + 1) {
MSG msg;
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
}
lpGIT->Release();
DeleteCriticalSection(&cs);
printf("This is after all threads exited\n");
CoUninitialize();
return 0;
}