Kill child thread (win32 api)

Posted on 2005-05-09
Last Modified: 2013-11-13

Me here with another API question.

I'm using CreateThread() to make a child thread for a given processes. However, I want to know how to be able to kill this thread by making it enter some cleanup routine, and then die peacefully. Normally this would just be about signalling, but it seems rather complex since I'm dealing with a multithreaded processes. I only want the child to die, and before it ends, it must run a function.

Help appreciaited

Question by:nerdmike
    LVL 23

    Expert Comment

    When the thread is created, you can pass it a pointer to a parameter.  Make that parameter a pointer to a bool, which when set, tells the thread it's time to go away.  Of course, the thread must regularly check that flag.
    LVL 32

    Accepted Solution

    Or, just create a global variable that is accessible to all threads, and have the child thread check it every so often.

    Author Comment

    Won't that create problems though

    I'm not too familiar with whats going on under the hood, but isnt it a bad idea to have to threads potentially accessing the same address simulatously?

    LVL 48

    Expert Comment

    Every worker thread must have stop event associated with it. When parent thread wants to stop child worker thread, it signals stop event and waits when worker thread actually exits. Worker thread must periodically test it's stop event state, and exit immidiately when this event is signaled. Worker thread must be always responsive and exit quickly when it's stop event signaled.

    // parent thread code - start thread
    HANDLE hStopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
    HANDLE hThread = CreateThread(...);

    // child thread code
    while (...)    // some loop
           // do something ...

           // test for stop event
           if ( WaitForSingleObject(hStopEvent, 0) == WAIT_OBJECT_0 )    // request to stop
    // clean-up and return

    // parent thread code - stop thread
    SetEvent(hStopEvent);        // ask thread to stop
    WaitForSingleObject(hThread, INFINITE);    // wait when thread actually exits

    In real programs I never use INFINITE loop, instead of this I use some maximal timeout (number of seconds) and after this kill worker thread using TerminateThread and report bug (non-responsive worker thread is bug).

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    has22 challenge 11 56
    canBalance challenge 34 57
    drawing animated level bar based on numbers 3 47
    White board coding practice 3 33
    Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now