|
From: Raghunath Marella.S <ms...@ya...> - 2002-06-15 18:32:01
|
Hi,
I am new to multithreading in VC++. I wrote a simple
program to understand the functionality of threads. I
used CreateThread. Actually, I am creating three
threads, and the ThreadProc will print integers from 0
to 10 for each thread. But the program is not working
as expected. The print statements and the getch()
functions are not seem to be working. I am not getting
why this should be? Will any one please clarify why
this is happening so? The code has been attached.
**************************************************
#include <iostream.h>
#include <stdlib.h>
#include <windows.h>
#include <conio.h>
DWORD WINAPI ThreadProc(LPVOID );
void 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.";
}
}
}
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;
}
_getch();
return 0;
}
***********************************************
thanks & regards,
Raghunath
__________________________________________________
Do You Yahoo!?
Yahoo! - Official partner of 2002 FIFA World Cup
http://fifaworldcup.yahoo.com
|
|
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.
|
|
From: Raghunath Marella.S <ms...@ya...> - 2002-06-16 05:21:51
|
Thanks Abhijit.
Is there any function call like join() of java, in
C++.
regards,
Raghunath
--- Abhijit Shylanath <mu...@gm...> wrote:
> 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.
>
>
>
_______________________________________________________________
>
> Sponsored by:
> ThinkGeek at http://www.ThinkGeek.com/
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
>
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
__________________________________________________
Do You Yahoo!?
Yahoo! - Official partner of 2002 FIFA World Cup
http://fifaworldcup.yahoo.com
|
|
From: Abhijit S. <mu...@gm...> - 2002-06-16 06:55:43
|
> Is there any function call like join() of java, in
> C++.
GetExitCodeThread(HANDLE, LPDWORD exit_code) tells you the exit code of the
thread. exit_code is STILL_ACTIVE if the thread is running, the actual exit
code if it has completed. For example,
#include <iostream.h>
#include <windows.h>
DWORD WINAPI ThreadProc(LPVOID );
int main()
{
for(int i = 0;i<3;i++)
{
DWORD threadId;
DWORD exit_code;
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.";
do
{
GetExitCodeThread(hThread, &exit_code);
} while(exit_code == STILL_ACTIVE);
cout << "\n Thread " << i+1 << " has completed.\n";
}
}
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;
}
--------------------------
http://mudeth.tripod.com
|