Wednesday, April 14, 2010

Multithreading programming in Win32

Here is the simple application that creates 100 threads and each thread executes the StartThraed() function


DWORD WINAPI StartThread(LPVOID threadID)
{
int tid = atoi((char *)threadID);
cout< return 0;
}

int main()
{
HANDLE hThread1;
DWORD dwGenericThread;
char threadID[10];

for (int x = 0; x < 10; ++x)
{
for (int y = 0; y < 10; ++y)
{
sprintf(threadID, "%d", y*10+x);
hThread1 = CreateThread(NULL,0,StartThread,&threadID,0,&dwGenericThread);
if(hThread1 == NULL)
{
DWORD dwError = GetLastError();
cout<<"SCM:Error in Creating thread"< return 0;
}
WaitForSingleObject(hThread1,INFINITE);
CloseHandle(hThread1);
}
}
}

No comments: