none
Fails to terminate a thread that created inside the kernel mode device driver! Why? RRS feed

  • Question

  • Hi,

    I has written a device driver code in windows embedded compact 7. It is a kernel mode device driver and written in the form of stream interface method.

    The IST Thread was created when XXX_Open is called. An event that associated to this thread was created as well. The IST Thread is started by calling ResumeThread(), and once it started it will waitForSingleObject (event) for infinite.

    When XXX_Close or XXX_DeInit is called, the open context or even device context needs to be free/release. Thus, the IST Thread needs to be terminated in order to release the resources back to the system.

    Therefore, the TerminateThread(pDevice->hIntrThread) is called to terminate IST thread (the pDevice->hIntrThread is the handler of IST Thread). Unfortunately, the IST thread fails to terminate. The return of TerminateThread is always "ZERO" which indicate that the IST thread cannot be terminated.

    Can some one explain to me WHY and show me how to terminate a thread in kernel mode device driver.

    In fact, I did do a simple experiment which create a similar thread user mode application. I can simply terminate that thread by calling TerminateThread without any error. Therefore, i just wondering why the same thread termination method can be done well in user mode application and failure in kernel mode device driver layer?

    Can someone or an expertise help to explain such phenomenon?

    Thanks

    Regards

    Chris


    Thursday, February 9, 2012 8:59 AM

Answers

  • Hi Chris,

    It is not good to use TerminateThread API.

    The better way to exit from thread is set a boolean flag. When ever you want to exit that thread set the boolean flag.

    In the IST check if that flag is set, if set then exit the thread.

    When I write drivers every initialization of memory, Threads and etc...happens in Init. So, I have to clean everything only in DeInit.

    Regards,

    GSR


    Thursday, February 9, 2012 10:31 AM

All replies

  • Hi Chris,

    It is not good to use TerminateThread API.

    The better way to exit from thread is set a boolean flag. When ever you want to exit that thread set the boolean flag.

    In the IST check if that flag is set, if set then exit the thread.

    When I write drivers every initialization of memory, Threads and etc...happens in Init. So, I have to clean everything only in DeInit.

    Regards,

    GSR


    Thursday, February 9, 2012 10:31 AM
  • Hi GSR,

    I get what you mean. I did try and it is working fine.

    In fact, for troubleshooting i use the GetExitCodeThread (before and after the exit event signaled) to check whether the thread is still active or been terminated.

    I'm wondering why it is not good to use the TerminateThread API in kernel mode device driver? Could you clarify to me about this?

    Thanks

    Regards

    Chris

    Friday, February 10, 2012 6:11 AM
  • Hi,

    I am not prettu much clear on this.

    I suggest you to read the content in the following URL: http://msdn.microsoft.com/en-us/library/ee482776(v=winembedded.60).aspx and search for the Termiante Thread in that page.

    Regards,

    GSR

    Friday, February 10, 2012 6:58 AM
  • This is not a kernel vs user mode question.  It is not good to use TerminateThread to shutdown threads.   Only use it in extreme conditions, like when your thread won't shutdown normally.

    Bruce Eitman (eMVP)
    Senior Engineer
    Bruce.Eitman AT Eurotech DOT com
    My BLOG http://geekswithblogs.net/bruceeitman

    Eurotech Inc.
    www.Eurotech.com

    Friday, February 10, 2012 1:11 PM
    Moderator