CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2003
    Posts
    2

    Post 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

  2. #2
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    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
    Code:
    int main()
    I know that some compilers will compile the wrong version returning a 'void' as well...

  3. #3
    Join Date
    May 2002
    Location
    Somewhere over the rainbow
    Posts
    423
    you can alway wait untill the thread exits and than close then handle: WaitForSingleObject()
    you can try that, i thin it should work
    Bengi

  4. #4
    Join Date
    Aug 2002
    Location
    United States
    Posts
    729
    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 =)

  5. #5
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    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;
    }

  6. #6
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    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.

  7. #7
    Join Date
    Jun 2003
    Posts
    2
    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
  •  





Click Here to Expand Forum to Full Width

Featured