Results 1 to 3 of 3

Thread: Threads

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800

    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

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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
  •  



Click Here to Expand Forum to Full Width