CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    basic thread question

    Hi

    I have a question about when thread terminates.
    Say I have the following
    Code:
    void foo()
    {
        Thread(/* all the params */); // e.g. CreateThread()
    }
    
    int main()
    {
        foo();
    }
    My questions are;

    Q1. If I dont explicitly call ExitThread(), when does the process created by Thread() terminates?

    Q2. Do all the threads created inside main() end when the program exits
    without having called the functions that end the threads?

    Thanks alot for the help~

  2. #2
    Join Date
    Apr 2009
    Posts
    3

    Re: basic thread question

    Answer to Q1: All Thread must has a method run(). When the method run() finished, the thread ended.

    Answer to Q2: Other threads will not be terminated automatically when the main() method finishes. But I think all threads will be terminated when you call System.exit().

  3. #3
    Join Date
    Nov 2003
    Posts
    1,902

    Re: basic thread question

    Q1: Windows/Posix - Consider the thread "no longer running" when you return from the thread function.

    Q2: Windows(MSCRT)/Posix - Returning from main() is the same as calling exit() - which kills all threads and the process.

    gg

  4. #4
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: basic thread question

    thank you Syoleen and Codeplug!

    That makes it more clear.

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