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 end/stop/destroy a thread

How end/stop/destroy a thread

Scheduled Pinned Locked Moved C / C++ / MFC
question
6 Posts 5 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.
  • Z Offline
    Z Offline
    zahid_ash
    wrote on last edited by
    #1

    A thread created by using AfxBeginThread(RUNTIME_CLASS(CClientHandler)); can be end/destroyed by using AfxEndThread(..) , would it be called from inside of same thread. If the AfxEndThread will end the thread then would the object created inside of that thread will also be destroyed? Thanks Regards.

    S C _ D 4 Replies Last reply
    0
    • Z zahid_ash

      A thread created by using AfxBeginThread(RUNTIME_CLASS(CClientHandler)); can be end/destroyed by using AfxEndThread(..) , would it be called from inside of same thread. If the AfxEndThread will end the thread then would the object created inside of that thread will also be destroyed? Thanks Regards.

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      IMHO, the most cleaner way to exit a thread is simply to return from the thread. For example, if you have a loop within the thread, you can simply use a flag to signalate when the thread must exit. This flag can be set by another thread (by using thread-safe methods, like CCriticalSection).


      Cédric Moonen Software developer
      Charting control

      1 Reply Last reply
      0
      • Z zahid_ash

        A thread created by using AfxBeginThread(RUNTIME_CLASS(CClientHandler)); can be end/destroyed by using AfxEndThread(..) , would it be called from inside of same thread. If the AfxEndThread will end the thread then would the object created inside of that thread will also be destroyed? Thanks Regards.

        S Offline
        S Offline
        Stephen Hewitt
        wrote on last edited by
        #3

        AfxEndThread must be called from within the thread you wish to stop. Steve

        Z 1 Reply Last reply
        0
        • S Stephen Hewitt

          AfxEndThread must be called from within the thread you wish to stop. Steve

          Z Offline
          Z Offline
          zahid_ash
          wrote on last edited by
          #4

          would this stop release all the memory that the thread and its members objects have occupied Regards.

          1 Reply Last reply
          0
          • Z zahid_ash

            A thread created by using AfxBeginThread(RUNTIME_CLASS(CClientHandler)); can be end/destroyed by using AfxEndThread(..) , would it be called from inside of same thread. If the AfxEndThread will end the thread then would the object created inside of that thread will also be destroyed? Thanks Regards.

            _ Offline
            _ Offline
            _AnsHUMAN_
            wrote on last edited by
            #5

            A thread can be terminated in four ways: 1)The thread function returns. (This is highly recommended.) 2)The thread kills itself by calling the ExitThread function. (Avoid this method.) 3)A thread in the same or in another process calls the TerminateThread function. (Avoid this method.) 4)The process containing the thread terminates. (Avoid this method.) Option 1 ensures that all thread resources are cleaned up properly. Any and all C++ objects created in your thread function will be destroyed properly via their destructors. The operating system will properly free the memory used by the thread's stack. The system will set the thread's exit code (maintained in the thread's kernel object) to your thread function's return value. The system will decrement the usage count of the thread's kernel object. Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_

            1 Reply Last reply
            0
            • Z zahid_ash

              A thread created by using AfxBeginThread(RUNTIME_CLASS(CClientHandler)); can be end/destroyed by using AfxEndThread(..) , would it be called from inside of same thread. If the AfxEndThread will end the thread then would the object created inside of that thread will also be destroyed? Thanks Regards.

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              See here.


              "The largest fire starts but with the smallest spark." - David Crow

              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