Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to get a thread's exit code

How to get a thread's exit code

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelptutorial
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    masnu
    wrote on last edited by
    #1

    Hi guys, I'm creating a multi-threaded application using _beginthreadex() and _endthreadex(). If my thread executes correctly I call _endthreadex( 0 ) but if it doesn't I call _endthreadex( _<some error code>_ ). How can I capture the thread's exit code in the main thread? Thanks.

    _ R 2 Replies Last reply
    0
    • M masnu

      Hi guys, I'm creating a multi-threaded application using _beginthreadex() and _endthreadex(). If my thread executes correctly I call _endthreadex( 0 ) but if it doesn't I call _endthreadex( _<some error code>_ ). How can I capture the thread's exit code in the main thread? Thanks.

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      GetExitCodeThread[^]

      «_Superman_» I love work. It gives me something to do between weekends.
      Microsoft MVP (Visual C++)

      1 Reply Last reply
      0
      • M masnu

        Hi guys, I'm creating a multi-threaded application using _beginthreadex() and _endthreadex(). If my thread executes correctly I call _endthreadex( 0 ) but if it doesn't I call _endthreadex( _<some error code>_ ). How can I capture the thread's exit code in the main thread? Thanks.

        R Offline
        R Offline
        Rajesh R Subramanian
        wrote on last edited by
        #3

        masnu wrote:

        If my thread executes correctly I call _endthreadex( 0 )

        Just returning would be the best way to exit a thread.

        “Follow your bliss.” – Joseph Campbell

        M 1 Reply Last reply
        0
        • R Rajesh R Subramanian

          masnu wrote:

          If my thread executes correctly I call _endthreadex( 0 )

          Just returning would be the best way to exit a thread.

          “Follow your bliss.” – Joseph Campbell

          M Offline
          M Offline
          masnu
          wrote on last edited by
          #4

          Hi Rajesh, I know that returning from a thread will call _endthreadex() but everything I have read says that "terminating a thread with a call to endthread or _endthreadex helps to ensure proper recovery of resources allocated for the thread". Is it safe to assume that returning will accomplish this? Thanks, Paul

          R 2 Replies Last reply
          0
          • M masnu

            Hi Rajesh, I know that returning from a thread will call _endthreadex() but everything I have read says that "terminating a thread with a call to endthread or _endthreadex helps to ensure proper recovery of resources allocated for the thread". Is it safe to assume that returning will accomplish this? Thanks, Paul

            R Offline
            R Offline
            Rajesh R Subramanian
            wrote on last edited by
            #5

            Hi Paul,

            masnu wrote:

            but everything I have read says that

            The documentation is confusing (I presume you read the documentation? If there's some other source which recommends _endthreadex instead of just returning the control, please ignore it. It's wrong). The documentation could have better been: "Do not call _endthread() or ExitThread() or TerminateThread(), etc., on a thread that was created by a call to _beginthreadex(). That will lead to resource leak. If you have to explicitly "end" a thread that was created by _beginthreadex(), then you should only call _endthreadex(). But just returning from the function is a better alternative." (similarly, _endthread and _beginthread is a pair) It's been explained very elaborately on the book Windows via C/C++ (and probably in a couple of other books too). I remember Dr. Joseph Newcomer had written on this too, which I'm lazy to search for now.

            masnu wrote:

            Is it safe to assume that returning will accomplish this?

            Yes! The resource cleanup will happen perfectly if you simply return the control. To conclude, there's simply no reason to call _endthreadex (or any of their akin functions that explicitly "ends" a thread) from your code. Returning the control is the best thing to do! Let me know if you need more information.

            “Follow your bliss.” – Joseph Campbell

            1 Reply Last reply
            0
            • M masnu

              Hi Rajesh, I know that returning from a thread will call _endthreadex() but everything I have read says that "terminating a thread with a call to endthread or _endthreadex helps to ensure proper recovery of resources allocated for the thread". Is it safe to assume that returning will accomplish this? Thanks, Paul

              R Offline
              R Offline
              Rajesh R Subramanian
              wrote on last edited by
              #6

              OK, I just searched Dr. Joseph's article and it's here: http://www.flounder.com/badprogram.htm#AfxExitThread[^] (He's a man that has earned a lot of respect in the community, and he says "Doing this will result in erroneous programs"). Now it's 12.59 AM here and I better hit the bed or I'll wake up late. Good night (probably good day) to you! :)

              “Follow your bliss.” – Joseph Campbell

              M 1 Reply Last reply
              0
              • R Rajesh R Subramanian

                OK, I just searched Dr. Joseph's article and it's here: http://www.flounder.com/badprogram.htm#AfxExitThread[^] (He's a man that has earned a lot of respect in the community, and he says "Doing this will result in erroneous programs"). Now it's 12.59 AM here and I better hit the bed or I'll wake up late. Good night (probably good day) to you! :)

                “Follow your bliss.” – Joseph Campbell

                M Offline
                M Offline
                masnu
                wrote on last edited by
                #7

                Thanks Rajesh. I appreciate the help. Paul

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups