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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. To kill a thread

To kill a thread

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
11 Posts 7 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 Mushtaque Nizamani

    I am calling a function in a thread that contains an infinite while loop and I want to stop that loop and as well as kill/exit thread on some condition. Can anyone please tell me how to do that?

    Regards, Mushq

    S Offline
    S Offline
    sashoalm
    wrote on last edited by
    #2

    TerminateThread will do the job, if you're not concerned about cleanup/releasing of resources.

    BOOL WINAPI TerminateThread(
    __inout HANDLE hThread,
    __in DWORD dwExitCode
    );

    S 1 Reply Last reply
    0
    • M Mushtaque Nizamani

      I am calling a function in a thread that contains an infinite while loop and I want to stop that loop and as well as kill/exit thread on some condition. Can anyone please tell me how to do that?

      Regards, Mushq

      J Offline
      J Offline
      JudyL_MD
      wrote on last edited by
      #3

      Use some kind of signalling mechanism between the thing that detects the termination condition and the thread. That infinite loop should have a check at the top for the condition. Easiest choice in windows is to use an event. Only use TerminateThread as an absolute last resort, only if you can't do it some other way. I've been multithreading for over 20 years and I have never had to use TerminateThread. It leaves your system in a mess, with all sorts of resources still allocated. Judy

      M 1 Reply Last reply
      0
      • M Mushtaque Nizamani

        I am calling a function in a thread that contains an infinite while loop and I want to stop that loop and as well as kill/exit thread on some condition. Can anyone please tell me how to do that?

        Regards, Mushq

        S Offline
        S Offline
        SandipG
        wrote on last edited by
        #4

        read this article if you cant resist use of TerminateThread() http://www.microsoft.com/msj/archive/SFFF.aspx[^]

        1 Reply Last reply
        0
        • J JudyL_MD

          Use some kind of signalling mechanism between the thing that detects the termination condition and the thread. That infinite loop should have a check at the top for the condition. Easiest choice in windows is to use an event. Only use TerminateThread as an absolute last resort, only if you can't do it some other way. I've been multithreading for over 20 years and I have never had to use TerminateThread. It leaves your system in a mess, with all sorts of resources still allocated. Judy

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #5

          JudyL_FL wrote:

          I've been multithreading for over 20 years

          Come on, Judy...since you were a toddler? I'm not buying it ;)

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          J 1 Reply Last reply
          0
          • M Mark Salsbery

            JudyL_FL wrote:

            I've been multithreading for over 20 years

            Come on, Judy...since you were a toddler? I'm not buying it ;)

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            J Offline
            J Offline
            JudyL_MD
            wrote on last edited by
            #6

            :-O :-\ :-O My very first job out of college had multithreading in it. Granted it was 8085 assembler inside a cash register instead of C on a windows PC but still ....

            1 Reply Last reply
            0
            • S sashoalm

              TerminateThread will do the job, if you're not concerned about cleanup/releasing of resources.

              BOOL WINAPI TerminateThread(
              __inout HANDLE hThread,
              __in DWORD dwExitCode
              );

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

              Bad advice. See here[^] for details (the documentation also warns against using it).

              Steve

              H 1 Reply Last reply
              0
              • S Stephen Hewitt

                Bad advice. See here[^] for details (the documentation also warns against using it).

                Steve

                H Offline
                H Offline
                Hamid Taebi
                wrote on last edited by
                #8

                I think its second times that somebody told about use of TerminateThread and you reply to him or her why its bad with a link. :-D

                S 1 Reply Last reply
                0
                • H Hamid Taebi

                  I think its second times that somebody told about use of TerminateThread and you reply to him or her why its bad with a link. :-D

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

                  Yeah, That sounds about right. It's one of those classic mistakes that even "professional" programmers make over and over again.

                  Steve

                  H 1 Reply Last reply
                  0
                  • S Stephen Hewitt

                    Yeah, That sounds about right. It's one of those classic mistakes that even "professional" programmers make over and over again.

                    Steve

                    H Offline
                    H Offline
                    Hamid Taebi
                    wrote on last edited by
                    #10

                    I personality like to read your replys because they arecomplete and helpful.

                    S 1 Reply Last reply
                    0
                    • H Hamid Taebi

                      I personality like to read your replys because they arecomplete and helpful.

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

                      Thank you.

                      Steve

                      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