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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with thread.

Status
Not open for further replies.

kathyayini

Programmer
Aug 16, 2002
52
IN
I am using thread to run Stored procedure which is in SQL Server.

for(IThreadCount=0;IThreadCount<3;IThreadCount++)
{
CreateThread(NULL,0,run_thread,(LPVOID)0,0,&dwThreadId[IThreadCount]);
SetThreadPriority(&dwThreadId[IThreadCount],THREAD_PRIORITY_HIGHEST);
Sleep(1000);
}

DWORD WINAPI run_thread(LPVOID Pntr)
{
try
{
Sleep(0);
TESTHR(pCommand.CreateInstance(__uuidof(Command)));

pCommand->ActiveConnection = pConnection;
pCommand->CommandText = (_bstr_t)ProcedureName[IThreadCount];
pCommand->CommandType = adCmdStoredProc;
pCommand->CommandTimeout = 15;

TESTHR(pParameter.CreateInstance(__uuidof(Parameter)));
pParameter->Type = adVarChar;
pParameter->Size = 3;
pParameter->Direction = adParamInput;
pParameter->Value = &quot;RG&quot;;
pCommand->Parameters->Append(pParameter);
pRecordset = pCommand->Execute(NULL,NULL,adCmdStoredProc);
success_flag=success_flag+1;
return TRUE; // thread completed successfully
}
catch(_com_error &f)
{
fail_flag=fail_flag+1;
return FALSE;
}
}

while((success_flag<IThreads) && (fail_flag==0))
{
//This loop is just to wait till all threads gets over.
}

for loop is to run 3 seperate threads which calls same function run_thread. while loop is to make the program wait till all the thread gets over. Problem is if i remove Sleep(1000) statement or make 1000 to 100, the program will not execute all threads properly. It will not set success_flag or fail_flag properly. The program suppose to increment success_flag,if thread executes Stored Procedure successfully else it will increment fail_flag flag. But it is not setting properly if i write Sleep(100) instead of Sleep(1000).
waiting for the reply
 
First: The definition for SetThreadPriority is:
BOOL SetThreadPriority(
HANDLE hThread, // handle to the thread
int nPriority // thread priority level
);
The first parameter (HANDLE hThread) must be a handle to a thread, this is NOT the thread-id. So the SetThreadPriority calls will fail with the result the created threads run the same priority as the calling thread.
Fix: The return value of CreateThread is a handle.
Change to something like:
HANDLE hThread[3];
for(IThreadCount=0;IThreadCount<3;IThreadCount++)
{
hThread[IThreadCount] = CreateThread(NULL,0,run_thread,(LPVOID)0,0,&dwThreadId[IThreadCount]);
SetThreadPriority(hThread[IThreadCount],THREAD_PRIORITY_HIGHEST);
Sleep(1000);
}

Second:
while((success_flag<IThreads) && (fail_flag==0))
{
//This loop is just to wait till all threads gets over.
}
This loop will take all the cpu-time unless other threads have higher priority (which they do not have, as we've noticed). Putting Sleep ( 10 ); in the while loop will give other threads a better chance.


A better solution:

1. declare a global DWORD variable initialized to zero.
2. Declare two global HANDLE variables.
3. with the first handle, call CreateMutex.
4. with the second handle, call CreateEvent.
5. create the three threads
6. wait for the event
7. close the handles to the mutex, event as well as the threads

the threads should:
1. Perform the com-tasks they should do
2. Wait for the mutex
3. add 1 to the global DWORD variable
4. Release the mutex
5. if the global DWORD variable == 3, fire the event
Marcel
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top