|
-
Jul 5th, 2002, 12:24 PM
#1
Thread Starter
Frenzied Member
Threads
I found an example on threads but I'm having trouble ripping it to a new project....
Code:
#include <iostream.h>
#include <fstream.h>
#include <time.h>
#include <windows.h>
HANDLE HThread;
unsigned long ThreadH;
unsigned long _stdcall getchthread(void* data);
int main()
{
HThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)getchthread,(LPVOID)0,0,&ThreadH);
TerminateThread(HThread,0);
return 0;
}
unsigned long _stdcall getchthread(void* data)
{
while(1)
{
cout<<"HELLO"<<endl;
}
return 0;
}
what am I missing
-
Jul 5th, 2002, 02:24 PM
#2
Monday Morning Lunatic
Well, it did exactly what I expected it to do (nothing ).
You don't give the thread time to actually *do* anything. Try this (with some minor tweaks):
Code:
#include <iostream>
#include <fstream>
#include <ctime>
#include <windows.h>
using namespace std;
HANDLE HThread;
unsigned long ThreadH;
unsigned long _stdcall getchthread(void* data);
int main() {
HThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)getchthread,(LPVOID)0,0,&ThreadH);
Sleep(3000);
TerminateThread(HThread,0);
return 0;
}
unsigned long _stdcall getchthread(void* data) {
while(1) {
cout << "HELLO" << endl;
Sleep(500);
}
return 0;
}
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jul 10th, 2002, 07:38 PM
#3
You shouldn't terminate a thread using TerminateThread. You should rather send a signal like here:
Code:
#include <iostream>
#include <fstream>
#include <ctime>
#include <windows.h>
using namespace std;
HANDLE HThread;
unsigned long ThreadH;
unsigned long _stdcall getchthread(void* data);
int main() {
bool *pEnd = new bool;
*pEnd = false;
HThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)getchthread,pEnd,0,&ThreadH);
Sleep(3000);
*pEnd = true;
// allow thread to terminate
WaitForSingleObject(HThread, INFINITE);
return 0;
}
unsigned long _stdcall getchthread(void* data) {
while(!*(bool*)data) {
cout << "HELLO" << endl;
Sleep(500);
}
return 0;
}
There is a small difference between parksie's includes and yours: the extension. You way is deprecated, use the new one.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
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
|