|
From: Abhijit S. <mu...@gm...> - 2002-06-16 05:09:07
|
You're not giving enough time for the threads to start. As soon as you
create them, the main() process ends, so the threads are destroyed. Put a
delay, either using a Sleep() or MessageBox() function, as show below. Try
clicking the OK button in the 'waiting' message-box to see what happens to
the threads, they're stopped without being completed. The new code is here:
#include <iostream.h>
#include <stdlib.h>
#include <windows.h>
#include <conio_mingw.h>
DWORD WINAPI ThreadProc(LPVOID );
int main()
{
for(int i = 0;i<3;i++)
{
DWORD threadId;
HANDLE hThread;
hThread = CreateThread(NULL,0,ThreadProc,&i,0,&threadId);
if(hThread == NULL)
cout << "\n Thread has not been created";
else
{
//_getch();
cout << "\n Thread has been created successfully.";
}
}
MessageBox(NULL, "Click to close.", "Waiting", MB_OK);
return 0;
}
DWORD WINAPI ThreadProc( LPVOID lpParam )
{
cout << "\n Entered threadProc...";
MessageBox(NULL,"sample output","",MB_OK);
for(int i = 0;i<10;i++)
{
cout << "\n thread output is:"<<HIWORD(lpParam) << ".."<<i;
}
return 0;
}
PS: Note that using void main() is WRONG!!! It should be int main(), and int
only. It may run on some platforms, but it won't run on all. Also, in MingW,
you have to use conio_mingw.h, but this is very platform and compiler
specific.
|