-
June 4th, 2003, 12:30 AM
#1
Threads question
just got this piece of code... wonder why it doesnt work
Code:
DWORD WINAPI ThreadFunc(LPVOID data)
{
printf("Hello!\n");
return 0;
}
void main()
{
HANDLE hThread;
DWORD dwThreadID;
hThread= CreateThread(NULL,
0,
ThreadFunc,
0,
0,
&dwThreadID);
CloseHandle(hThread);
}
Any help appreciated
-
June 4th, 2003, 01:31 AM
#2
Well...since you did not specify the problem I have to guess...however, I assume that you do not see the output?
The reason for that is that you immediately invalidates the handle of the to be created thread...then your application immediately exists...
Code:
#include <conio.h>
DWORD WINAPI ThreadFunc(LPVOID data)
{
printf("Hello!\n");
return 0;
}
int main()
{
HANDLE hThread;
DWORD dwThreadID;
hThread= CreateThread(0, 0, ThreadFunc, 0, 0, &dwThreadID);
// Wait for keystroke
_getch();
CloseHandle(hThread);
return 0;
}
This will give the thread enough time to be created...it will print out the line while the application is waiting for a keystroke...
Besides that, the prototype for the 'main()' function is
I know that some compilers will compile the wrong version returning a 'void' as well...
-
June 4th, 2003, 02:10 AM
#3
you can alway wait untill the thread exits and than close then handle: WaitForSingleObject()
you can try that, i thin it should work
Bengi
-
June 4th, 2003, 02:13 AM
#4
to elaborate on what andreas said....
the text doesn't show because your program terminates before the output can be displayed.
andreas, it was my understanding that closing the thread handle wont kill the thread but simply make it inaccessable. is this right? iow, the thread still runs after CloseHandle the app just doesn't have a way of accessing it so the problem with the example is that the prog itself quits first.
you could also use
WaitForSingleObject(hThread, time_to_wait_in_milliseconds);
if you dont want/require a keystroke.
-edit, yeah heh love it when multiple posts come in at the same time =)
-
June 4th, 2003, 06:33 AM
#5
Originally posted by Bengi
you can alway wait untill the thread exits and than close then handle: WaitForSingleObject()
you can try that, i thin it should work
Well..sometimes I do not see the obvious solution...
Waiting on the thread handle is much better than adding the keystroke functionality...thus
Code:
DWORD WINAPI ThreadFunc(LPVOID data)
{
printf("Hello!\n");
return 0;
}
int main()
{
HANDLE hThread;
DWORD dwThreadID;
hThread= CreateThread(0, 0, ThreadFunc, 0, 0, &dwThreadID);
// Wait for thread
WaitForSingleObject(hThread, INFINITE);
CloseHandle(hThread);
return 0;
}
-
June 4th, 2003, 06:36 AM
#6
Originally posted by filthy_mcnasty
andreas, it was my understanding that closing the thread handle wont kill the thread but simply make it inaccessable. is this right? iow, the thread still runs after CloseHandle the app just doesn't have a way of accessing it so the problem with the example is that the prog itself quits first.
Yes, that is what I basically meant...'CloseHandle()' does not close the thread object itself, the reason for not getting the output is indeed the closing of the application without giving the thread a chance to be created and run.
I might have said this in clearer words...sorry for that.
-
June 4th, 2003, 07:59 AM
#7
you guys are great :-D
just getting into threads now as I have a chat proggie I wanna get together and multithreading seems to be the way to go. Once again, thank you all.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|